1 //===- CallGraph.cpp - Build a Module's call graph ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the CallGraph class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/CallGraph.h"
15 #include "llvm/Constants.h" // Remove when ConstantPointerRefs are gone
16 #include "llvm/Module.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/Support/CallSite.h"
19 #include "Support/STLExtras.h"
22 static RegisterAnalysis<CallGraph> X("callgraph", "Call Graph Construction");
24 // getNodeFor - Return the node for the specified function or create one if it
25 // does not already exist.
27 CallGraphNode *CallGraph::getNodeFor(Function *F) {
28 CallGraphNode *&CGN = FunctionMap[F];
31 assert((!F || F->getParent() == Mod) && "Function not in current module!");
32 return CGN = new CallGraphNode(F);
35 static bool isOnlyADirectCall(Function *F, CallSite CS) {
36 if (!CS.getInstruction()) return false;
37 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; ++I)
38 if (*I == F) return false;
42 // addToCallGraph - Add a function to the call graph, and link the node to all
43 // of the functions that it calls.
45 void CallGraph::addToCallGraph(Function *F) {
46 CallGraphNode *Node = getNodeFor(F);
48 // If this function has external linkage, anything could call it...
49 if (!F->hasInternalLinkage()) {
50 ExternalCallingNode->addCalledFunction(Node);
52 // Found the entry point?
53 if (F->getName() == "main") {
54 if (Root) // Found multiple external mains? Don't pick one.
55 Root = ExternalCallingNode;
57 Root = Node; // Found a main, keep track of it!
61 // If this function is not defined in this translation unit, it could call
63 if (F->isExternal() && !F->getIntrinsicID())
64 Node->addCalledFunction(CallsExternalNode);
66 // Loop over all of the users of the function... looking for callers...
68 bool isUsedExternally = false;
69 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I) {
70 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
71 if (isOnlyADirectCall(F, CallSite::get(Inst)))
72 getNodeFor(Inst->getParent()->getParent())->addCalledFunction(Node);
74 isUsedExternally = true;
75 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(*I)) {
76 for (Value::use_iterator I = GV->use_begin(), E = GV->use_end();
78 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
79 if (isOnlyADirectCall(F, CallSite::get(Inst)))
80 getNodeFor(Inst->getParent()->getParent())->addCalledFunction(Node);
82 isUsedExternally = true;
84 isUsedExternally = true;
86 } else { // Can't classify the user!
87 isUsedExternally = true;
91 ExternalCallingNode->addCalledFunction(Node);
93 // Look for an indirect function call...
94 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
95 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II){
96 CallSite CS = CallSite::get(II);
97 if (CS.getInstruction() && !CS.getCalledFunction())
98 Node->addCalledFunction(CallsExternalNode);
102 bool CallGraph::run(Module &M) {
106 ExternalCallingNode = getNodeFor(0);
107 CallsExternalNode = new CallGraphNode(0);
110 // Add every function to the call graph...
111 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
114 // If we didn't find a main function, use the external call graph node
115 if (Root == 0) Root = ExternalCallingNode;
120 void CallGraph::destroy() {
121 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
125 delete CallsExternalNode;
126 CallsExternalNode = 0;
129 static void WriteToOutput(const CallGraphNode *CGN, std::ostream &o) {
130 if (CGN->getFunction())
131 o << "Call graph node for function: '"
132 << CGN->getFunction()->getName() <<"'\n";
134 o << "Call graph node <<null function: 0x" << CGN << ">>:\n";
136 for (unsigned i = 0; i < CGN->size(); ++i)
137 if ((*CGN)[i]->getFunction())
138 o << " Calls function '" << (*CGN)[i]->getFunction()->getName() << "'\n";
140 o << " Calls external node\n";
144 void CallGraph::print(std::ostream &o, const Module *M) const {
145 o << "CallGraph Root is: ";
146 if (getRoot()->getFunction())
147 o << getRoot()->getFunction()->getName() << "\n";
149 o << "<<null function: 0x" << getRoot() << ">>\n";
151 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
152 WriteToOutput(I->second, o);
156 //===----------------------------------------------------------------------===//
157 // Implementations of public modification methods
160 // Functions to keep a call graph up to date with a function that has been
163 void CallGraph::addFunctionToModule(Function *F) {
164 assert(0 && "not implemented");
168 // removeFunctionFromModule - Unlink the function from this module, returning
169 // it. Because this removes the function from the module, the call graph node
170 // is destroyed. This is only valid if the function does not call any other
171 // functions (ie, there are no edges in it's CGN). The easiest way to do this
172 // is to dropAllReferences before calling this.
174 Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
175 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
176 "graph if it references other functions!");
177 Function *F = CGN->getFunction(); // Get the function for the call graph node
178 delete CGN; // Delete the call graph node for this func
179 FunctionMap.erase(F); // Remove the call graph node from the map
181 Mod->getFunctionList().remove(F);
185 void CallGraph::stub() {}
187 void CallGraphNode::removeCallEdgeTo(CallGraphNode *Callee) {
188 for (unsigned i = CalledFunctions.size(); ; --i) {
189 assert(i && "Cannot find callee to remove!");
190 if (CalledFunctions[i-1] == Callee) {
191 CalledFunctions.erase(CalledFunctions.begin()+i-1);