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 and provides the BasicCallGraph
11 // default implementation.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/CallGraph.h"
16 #include "llvm/Module.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/Support/CallSite.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/Streams.h"
24 /// isOnlyADirectCall - Return true if this callsite is *just* a direct call to
25 /// the specified function. Specifically return false if the callsite also
26 /// takes the address of the function.
27 static bool isOnlyADirectCall(Function *F, CallSite CS) {
28 if (!CS.getInstruction()) return false;
29 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; ++I)
30 if (*I == F) return false;
36 //===----------------------------------------------------------------------===//
37 // BasicCallGraph class definition
39 class VISIBILITY_HIDDEN BasicCallGraph : public CallGraph, public ModulePass {
40 // Root is root of the call graph, or the external node if a 'main' function
45 // ExternalCallingNode - This node has edges to all external functions and
46 // those internal functions that have their address taken.
47 CallGraphNode *ExternalCallingNode;
49 // CallsExternalNode - This node has edges to it from all functions making
50 // indirect calls or calling an external function.
51 CallGraphNode *CallsExternalNode;
54 static char ID; // Class identification, replacement for typeinfo
55 BasicCallGraph() : ModulePass((intptr_t)&ID), Root(0),
56 ExternalCallingNode(0), CallsExternalNode(0) {}
58 // runOnModule - Compute the call graph for the specified module.
59 virtual bool runOnModule(Module &M) {
60 CallGraph::initialize(M);
62 ExternalCallingNode = getOrInsertFunction(0);
63 CallsExternalNode = new CallGraphNode(0);
66 // Add every function to the call graph...
67 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
70 // If we didn't find a main function, use the external call graph node
71 if (Root == 0) Root = ExternalCallingNode;
76 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
80 void print(std::ostream *o, const Module *M) const {
84 virtual void print(std::ostream &o, const Module *M) const {
85 o << "CallGraph Root is: ";
86 if (Function *F = getRoot()->getFunction())
87 o << F->getName() << "\n";
89 o << "<<null function: 0x" << getRoot() << ">>\n";
91 CallGraph::print(o, M);
94 virtual void releaseMemory() {
98 /// dump - Print out this call graph.
100 inline void dump() const {
104 CallGraphNode* getExternalCallingNode() const { return ExternalCallingNode; }
105 CallGraphNode* getCallsExternalNode() const { return CallsExternalNode; }
107 // getRoot - Return the root of the call graph, which is either main, or if
108 // main cannot be found, the external node.
110 CallGraphNode *getRoot() { return Root; }
111 const CallGraphNode *getRoot() const { return Root; }
114 //===---------------------------------------------------------------------
115 // Implementation of CallGraph construction
118 // addToCallGraph - Add a function to the call graph, and link the node to all
119 // of the functions that it calls.
121 void addToCallGraph(Function *F) {
122 CallGraphNode *Node = getOrInsertFunction(F);
124 // If this function has external linkage, anything could call it.
125 if (!F->hasInternalLinkage()) {
126 ExternalCallingNode->addCalledFunction(CallSite(), Node);
128 // Found the entry point?
129 if (F->getName() == "main") {
130 if (Root) // Found multiple external mains? Don't pick one.
131 Root = ExternalCallingNode;
133 Root = Node; // Found a main, keep track of it!
137 // If this function is not defined in this translation unit, it could call
139 if (F->isDeclaration() && !F->getIntrinsicID())
140 Node->addCalledFunction(CallSite(), CallsExternalNode);
142 // Loop over all of the users of the function... looking for callers...
144 bool isUsedExternally = false;
145 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I){
146 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
147 CallSite CS = CallSite::get(Inst);
148 if (isOnlyADirectCall(F, CS))
149 getOrInsertFunction(Inst->getParent()->getParent())
150 ->addCalledFunction(CS, Node);
152 isUsedExternally = true;
153 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(*I)) {
154 for (Value::use_iterator I = GV->use_begin(), E = GV->use_end();
156 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
157 CallSite CS = CallSite::get(Inst);
158 if (isOnlyADirectCall(F, CS))
159 getOrInsertFunction(Inst->getParent()->getParent())
160 ->addCalledFunction(CS, Node);
162 isUsedExternally = true;
164 isUsedExternally = true;
166 } else { // Can't classify the user!
167 isUsedExternally = true;
170 if (isUsedExternally)
171 ExternalCallingNode->addCalledFunction(CallSite(), Node);
173 // Look for an indirect function call.
174 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
175 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
177 CallSite CS = CallSite::get(II);
178 if (CS.getInstruction() && !CS.getCalledFunction())
179 Node->addCalledFunction(CS, CallsExternalNode);
184 // destroy - Release memory for the call graph
185 virtual void destroy() {
186 /// CallsExternalNode is not in the function map, delete it explicitly.
187 delete CallsExternalNode;
188 CallsExternalNode = 0;
189 CallGraph::destroy();
193 RegisterAnalysisGroup<CallGraph> X("Call Graph");
194 RegisterPass<BasicCallGraph> Y("basiccg", "Basic CallGraph Construction");
195 RegisterAnalysisGroup<CallGraph, true> Z(Y);
197 } //End anonymous namespace
199 char CallGraph::ID = 0;
200 char BasicCallGraph::ID = 0;
202 void CallGraph::initialize(Module &M) {
206 void CallGraph::destroy() {
207 if (!FunctionMap.empty()) {
208 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
215 void CallGraph::print(std::ostream &OS, const Module *M) const {
216 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
217 I->second->print(OS);
220 void CallGraph::dump() const {
224 //===----------------------------------------------------------------------===//
225 // Implementations of public modification methods
228 // removeFunctionFromModule - Unlink the function from this module, returning
229 // it. Because this removes the function from the module, the call graph node
230 // is destroyed. This is only valid if the function does not call any other
231 // functions (ie, there are no edges in it's CGN). The easiest way to do this
232 // is to dropAllReferences before calling this.
234 Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
235 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
236 "graph if it references other functions!");
237 Function *F = CGN->getFunction(); // Get the function for the call graph node
238 delete CGN; // Delete the call graph node for this func
239 FunctionMap.erase(F); // Remove the call graph node from the map
241 Mod->getFunctionList().remove(F);
245 // changeFunction - This method changes the function associated with this
246 // CallGraphNode, for use by transformations that need to change the prototype
247 // of a Function (thus they must create a new Function and move the old code
249 void CallGraph::changeFunction(Function *OldF, Function *NewF) {
250 iterator I = FunctionMap.find(OldF);
251 CallGraphNode *&New = FunctionMap[NewF];
252 assert(I != FunctionMap.end() && I->second && !New &&
253 "OldF didn't exist in CG or NewF already does!");
256 FunctionMap.erase(I);
259 // getOrInsertFunction - This method is identical to calling operator[], but
260 // it will insert a new CallGraphNode for the specified function if one does
261 // not already exist.
262 CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
263 CallGraphNode *&CGN = FunctionMap[F];
266 assert((!F || F->getParent() == Mod) && "Function not in current module!");
267 return CGN = new CallGraphNode(const_cast<Function*>(F));
270 void CallGraphNode::print(std::ostream &OS) const {
271 if (Function *F = getFunction())
272 OS << "Call graph node for function: '" << F->getName() <<"'\n";
274 OS << "Call graph node <<null function: 0x" << this << ">>:\n";
276 for (const_iterator I = begin(), E = end(); I != E; ++I)
277 if (I->second->getFunction())
278 OS << " Calls function '" << I->second->getFunction()->getName() <<"'\n";
280 OS << " Calls external node\n";
284 void CallGraphNode::dump() const { print(cerr); }
286 void CallGraphNode::removeCallEdgeTo(CallGraphNode *Callee) {
287 for (unsigned i = CalledFunctions.size(); ; --i) {
288 assert(i && "Cannot find callee to remove!");
289 if (CalledFunctions[i-1].second == Callee) {
290 CalledFunctions.erase(CalledFunctions.begin()+i-1);
296 // removeAnyCallEdgeTo - This method removes any call edges from this node to
297 // the specified callee function. This takes more time to execute than
298 // removeCallEdgeTo, so it should not be used unless necessary.
299 void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
300 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
301 if (CalledFunctions[i].second == Callee) {
302 CalledFunctions[i] = CalledFunctions.back();
303 CalledFunctions.pop_back();
308 // Enuse that users of CallGraph.h also link with this file
309 DEFINING_FILE_FOR(CallGraph)