blob: 65c7c6efd8027e74ee5931d13c72eebf1aceba86 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- CallGraph.cpp - Build a Module's call graph ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the CallGraph class and provides the BasicCallGraph
11// default implementation.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/CallGraph.h"
16#include "llvm/Module.h"
17#include "llvm/Instructions.h"
Dale Johannesenb618d082009-03-19 18:03:56 +000018#include "llvm/IntrinsicInst.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/Support/CallSite.h"
David Greene6e554392009-12-23 20:03:58 +000020#include "llvm/Support/Debug.h"
Chris Lattner397f4562009-08-23 06:03:38 +000021#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022using namespace llvm;
23
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024namespace {
25
26//===----------------------------------------------------------------------===//
27// BasicCallGraph class definition
28//
Chris Lattnerb8327c92010-01-20 19:51:46 +000029class BasicCallGraph : public ModulePass, public CallGraph {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030 // Root is root of the call graph, or the external node if a 'main' function
31 // couldn't be found.
32 //
33 CallGraphNode *Root;
34
35 // ExternalCallingNode - This node has edges to all external functions and
36 // those internal functions that have their address taken.
37 CallGraphNode *ExternalCallingNode;
38
39 // CallsExternalNode - This node has edges to it from all functions making
40 // indirect calls or calling an external function.
41 CallGraphNode *CallsExternalNode;
42
43public:
44 static char ID; // Class identification, replacement for typeinfo
Dan Gohman26f8c272008-09-04 17:05:41 +000045 BasicCallGraph() : ModulePass(&ID), Root(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 ExternalCallingNode(0), CallsExternalNode(0) {}
47
48 // runOnModule - Compute the call graph for the specified module.
49 virtual bool runOnModule(Module &M) {
50 CallGraph::initialize(M);
51
52 ExternalCallingNode = getOrInsertFunction(0);
53 CallsExternalNode = new CallGraphNode(0);
54 Root = 0;
55
Chris Lattnerfd707922009-08-31 03:15:49 +000056 // Add every function to the call graph.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
58 addToCallGraph(I);
59
60 // If we didn't find a main function, use the external call graph node
61 if (Root == 0) Root = ExternalCallingNode;
62
63 return false;
64 }
65
66 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
67 AU.setPreservesAll();
68 }
69
Chris Lattner397f4562009-08-23 06:03:38 +000070 virtual void print(raw_ostream &OS, const Module *) const {
71 OS << "CallGraph Root is: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 if (Function *F = getRoot()->getFunction())
Chris Lattner397f4562009-08-23 06:03:38 +000073 OS << F->getName() << "\n";
74 else {
75 OS << "<<null function: 0x" << getRoot() << ">>\n";
76 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077
Chris Lattner397f4562009-08-23 06:03:38 +000078 CallGraph::print(OS, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 }
80
81 virtual void releaseMemory() {
82 destroy();
83 }
84
Chris Lattnerb8327c92010-01-20 19:51:46 +000085 /// getAdjustedAnalysisPointer - This method is used when a pass implements
86 /// an analysis interface through multiple inheritance. If needed, it should
87 /// override this to adjust the this pointer as needed for the specified pass
88 /// info.
Owen Andersoncb14cb82010-07-20 08:26:15 +000089 virtual void *getAdjustedAnalysisPointer(const PassInfo *PI) {
Chris Lattnerb8327c92010-01-20 19:51:46 +000090 if (PI->isPassID(&CallGraph::ID))
91 return (CallGraph*)this;
92 return this;
93 }
94
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 CallGraphNode* getExternalCallingNode() const { return ExternalCallingNode; }
96 CallGraphNode* getCallsExternalNode() const { return CallsExternalNode; }
97
98 // getRoot - Return the root of the call graph, which is either main, or if
99 // main cannot be found, the external node.
100 //
101 CallGraphNode *getRoot() { return Root; }
102 const CallGraphNode *getRoot() const { return Root; }
103
104private:
105 //===---------------------------------------------------------------------
106 // Implementation of CallGraph construction
107 //
108
109 // addToCallGraph - Add a function to the call graph, and link the node to all
110 // of the functions that it calls.
111 //
112 void addToCallGraph(Function *F) {
113 CallGraphNode *Node = getOrInsertFunction(F);
114
115 // If this function has external linkage, anything could call it.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000116 if (!F->hasLocalLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 ExternalCallingNode->addCalledFunction(CallSite(), Node);
118
119 // Found the entry point?
120 if (F->getName() == "main") {
121 if (Root) // Found multiple external mains? Don't pick one.
122 Root = ExternalCallingNode;
123 else
124 Root = Node; // Found a main, keep track of it!
125 }
126 }
127
Duncan Sands3ebfa912008-09-09 19:56:34 +0000128 // Loop over all of the users of the function, looking for non-call uses.
Gabor Greif373d5a32010-07-09 13:17:13 +0000129 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I){
130 User *U = *I;
131 if ((!isa<CallInst>(U) && !isa<InvokeInst>(U))
132 || !CallSite(cast<Instruction>(U)).isCallee(I)) {
Duncan Sands3ebfa912008-09-09 19:56:34 +0000133 // Not a call, or being used as a parameter rather than as the callee.
134 ExternalCallingNode->addCalledFunction(CallSite(), Node);
135 break;
136 }
Gabor Greif373d5a32010-07-09 13:17:13 +0000137 }
Duncan Sands3ebfa912008-09-09 19:56:34 +0000138
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 // If this function is not defined in this translation unit, it could call
140 // anything.
Duncan Sands79d28872007-12-03 20:06:50 +0000141 if (F->isDeclaration() && !F->isIntrinsic())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 Node->addCalledFunction(CallSite(), CallsExternalNode);
143
Duncan Sands8f7eaea2008-09-09 12:40:47 +0000144 // Look for calls by this function.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
146 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
147 II != IE; ++II) {
Duncan Sands8f7eaea2008-09-09 12:40:47 +0000148 CallSite CS = CallSite::get(II);
Dale Johannesenb618d082009-03-19 18:03:56 +0000149 if (CS.getInstruction() && !isa<DbgInfoIntrinsic>(II)) {
Duncan Sands8f7eaea2008-09-09 12:40:47 +0000150 const Function *Callee = CS.getCalledFunction();
151 if (Callee)
152 Node->addCalledFunction(CS, getOrInsertFunction(Callee));
153 else
154 Node->addCalledFunction(CS, CallsExternalNode);
155 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 }
157 }
158
159 //
160 // destroy - Release memory for the call graph
161 virtual void destroy() {
162 /// CallsExternalNode is not in the function map, delete it explicitly.
Benjamin Kramer5e814112010-04-20 12:16:50 +0000163 if (CallsExternalNode) {
164 CallsExternalNode->allReferencesDropped();
165 delete CallsExternalNode;
166 CallsExternalNode = 0;
167 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 CallGraph::destroy();
169 }
170};
171
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172} //End anonymous namespace
173
Dan Gohman089efff2008-05-13 00:00:25 +0000174static RegisterAnalysisGroup<CallGraph> X("Call Graph");
175static RegisterPass<BasicCallGraph>
176Y("basiccg", "Basic CallGraph Construction", false, true);
177static RegisterAnalysisGroup<CallGraph, true> Z(Y);
178
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179char CallGraph::ID = 0;
180char BasicCallGraph::ID = 0;
181
182void CallGraph::initialize(Module &M) {
183 Mod = &M;
184}
185
186void CallGraph::destroy() {
Chris Lattnerfd707922009-08-31 03:15:49 +0000187 if (FunctionMap.empty()) return;
188
Chris Lattner01b08ae2010-04-20 00:47:34 +0000189 // Reset all node's use counts to zero before deleting them to prevent an
190 // assertion from firing.
191#ifndef NDEBUG
192 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
193 I != E; ++I)
194 I->second->allReferencesDropped();
195#endif
196
Chris Lattnerfd707922009-08-31 03:15:49 +0000197 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
198 I != E; ++I)
199 delete I->second;
200 FunctionMap.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201}
202
Chris Lattner397f4562009-08-23 06:03:38 +0000203void CallGraph::print(raw_ostream &OS, Module*) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
205 I->second->print(OS);
206}
Chris Lattnerba263e52009-08-30 22:24:32 +0000207void CallGraph::dump() const {
David Greene6e554392009-12-23 20:03:58 +0000208 print(dbgs(), 0);
Chris Lattnerba263e52009-08-30 22:24:32 +0000209}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211//===----------------------------------------------------------------------===//
212// Implementations of public modification methods
213//
214
215// removeFunctionFromModule - Unlink the function from this module, returning
216// it. Because this removes the function from the module, the call graph node
217// is destroyed. This is only valid if the function does not call any other
218// functions (ie, there are no edges in it's CGN). The easiest way to do this
219// is to dropAllReferences before calling this.
220//
221Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
Chris Lattner7d7776f2009-08-31 02:24:20 +0000222 assert(CGN->empty() && "Cannot remove function from call "
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 "graph if it references other functions!");
224 Function *F = CGN->getFunction(); // Get the function for the call graph node
225 delete CGN; // Delete the call graph node for this func
226 FunctionMap.erase(F); // Remove the call graph node from the map
227
228 Mod->getFunctionList().remove(F);
229 return F;
230}
231
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232// getOrInsertFunction - This method is identical to calling operator[], but
233// it will insert a new CallGraphNode for the specified function if one does
234// not already exist.
235CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
236 CallGraphNode *&CGN = FunctionMap[F];
237 if (CGN) return CGN;
238
239 assert((!F || F->getParent() == Mod) && "Function not in current module!");
240 return CGN = new CallGraphNode(const_cast<Function*>(F));
241}
242
Chris Lattner397f4562009-08-23 06:03:38 +0000243void CallGraphNode::print(raw_ostream &OS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 if (Function *F = getFunction())
Chris Lattnerfd707922009-08-31 03:15:49 +0000245 OS << "Call graph node for function: '" << F->getName() << "'";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 else
Chris Lattnerfd707922009-08-31 03:15:49 +0000247 OS << "Call graph node <<null function>>";
248
Chris Lattner01443f82010-04-23 18:23:40 +0000249 OS << "<<" << this << ">> #uses=" << getNumReferences() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250
Chris Lattner01443f82010-04-23 18:23:40 +0000251 for (const_iterator I = begin(), E = end(); I != E; ++I) {
252 OS << " CS<" << I->first << "> calls ";
Gabor Greifbd78fa82009-01-14 17:09:04 +0000253 if (Function *FI = I->second->getFunction())
Chris Lattner01443f82010-04-23 18:23:40 +0000254 OS << "function '" << FI->getName() <<"'\n";
255 else
256 OS << "external node\n";
257 }
258 OS << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259}
260
David Greene6e554392009-12-23 20:03:58 +0000261void CallGraphNode::dump() const { print(dbgs()); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262
Chris Lattner9125c402008-04-13 19:41:25 +0000263/// removeCallEdgeFor - This method removes the edge in the node for the
264/// specified call site. Note that this method takes linear time, so it
265/// should be used sparingly.
266void CallGraphNode::removeCallEdgeFor(CallSite CS) {
Chris Lattner298ea102009-08-31 07:23:46 +0000267 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
268 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
Chris Lattnera59b5de2009-09-01 06:31:31 +0000269 if (I->first == CS.getInstruction()) {
Chris Lattner298ea102009-08-31 07:23:46 +0000270 I->second->DropRef();
271 *I = CalledFunctions.back();
272 CalledFunctions.pop_back();
273 return;
274 }
Chris Lattner9125c402008-04-13 19:41:25 +0000275 }
276}
277
278
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279// removeAnyCallEdgeTo - This method removes any call edges from this node to
280// the specified callee function. This takes more time to execute than
281// removeCallEdgeTo, so it should not be used unless necessary.
282void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
283 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
284 if (CalledFunctions[i].second == Callee) {
Chris Lattnerfd707922009-08-31 03:15:49 +0000285 Callee->DropRef();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 CalledFunctions[i] = CalledFunctions.back();
287 CalledFunctions.pop_back();
288 --i; --e;
289 }
290}
291
Duncan Sands349bf2e2008-10-03 07:36:09 +0000292/// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite
293/// from this node to the specified callee function.
294void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) {
Gabor Greifa3ef1552009-01-17 19:46:01 +0000295 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
296 assert(I != CalledFunctions.end() && "Cannot find callee to remove!");
297 CallRecord &CR = *I;
Chris Lattnera59b5de2009-09-01 06:31:31 +0000298 if (CR.second == Callee && CR.first == 0) {
Chris Lattnerfd707922009-08-31 03:15:49 +0000299 Callee->DropRef();
300 *I = CalledFunctions.back();
301 CalledFunctions.pop_back();
Duncan Sands349bf2e2008-10-03 07:36:09 +0000302 return;
303 }
304 }
305}
306
Chris Lattner69177fb2009-09-15 05:40:35 +0000307/// replaceCallEdge - This method replaces the edge in the node for the
308/// specified call site with a new one. Note that this method takes linear
309/// time, so it should be used sparingly.
310void CallGraphNode::replaceCallEdge(CallSite CS,
311 CallSite NewCS, CallGraphNode *NewNode){
312 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
313 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
314 if (I->first == CS.getInstruction()) {
315 I->second->DropRef();
316 I->first = NewCS.getInstruction();
317 I->second = NewNode;
318 NewNode->AddRef();
319 return;
320 }
321 }
322}
323
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324// Enuse that users of CallGraph.h also link with this file
325DEFINING_FILE_FOR(CallGraph)