blob: f5cbc9413f15e35e57e5ee31c281ea1cbc410ef3 [file] [log] [blame]
Chris Lattnerd852cc32002-03-06 18:00:49 +00001//===- CallGraph.cpp - Build a Module's call graph ------------------------===//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman01808ca2005-04-21 21:13:18 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerbbf3ae82001-09-28 00:08:15 +00009//
Chris Lattnerbe1987772005-12-22 06:07:52 +000010// This file implements the CallGraph class and provides the BasicCallGraph
11// default implementation.
Chris Lattner8cbbbef2001-10-13 06:33:19 +000012//
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000013//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/CallGraph.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000016#include "llvm/Instructions.h"
Dale Johannesen20509682009-03-19 18:03:56 +000017#include "llvm/IntrinsicInst.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/Module.h"
Chris Lattner9c4a58b2003-10-31 21:05:12 +000019#include "llvm/Support/CallSite.h"
David Greeneba44b3e2009-12-23 20:03:58 +000020#include "llvm/Support/Debug.h"
Chris Lattner13626022009-08-23 06:03:38 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattner8b6db182004-04-12 05:36:32 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Chris Lattnerbe1987772005-12-22 06:07:52 +000024namespace {
25
26//===----------------------------------------------------------------------===//
27// BasicCallGraph class definition
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000028//
Chris Lattner397af342010-01-20 19:51:46 +000029class BasicCallGraph : public ModulePass, public CallGraph {
Chris Lattnerbe1987772005-12-22 06:07:52 +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;
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000034
Chris Lattnerbe1987772005-12-22 06:07:52 +000035 // ExternalCallingNode - This node has edges to all external functions and
36 // those internal functions that have their address taken.
37 CallGraphNode *ExternalCallingNode;
Chris Lattner03946cd2001-11-26 18:51:25 +000038
Chris Lattnerbe1987772005-12-22 06:07:52 +000039 // CallsExternalNode - This node has edges to it from all functions making
40 // indirect calls or calling an external function.
41 CallGraphNode *CallsExternalNode;
42
43public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000044 static char ID; // Class identification, replacement for typeinfo
Owen Andersona7aed182010-08-06 18:33:48 +000045 BasicCallGraph() : ModulePass(ID), Root(0),
Owen Anderson6c18d1a2010-10-19 17:21:58 +000046 ExternalCallingNode(0), CallsExternalNode(0) {
47 initializeBasicCallGraphPass(*PassRegistry::getPassRegistry());
48 }
Chris Lattnerbe1987772005-12-22 06:07:52 +000049
50 // runOnModule - Compute the call graph for the specified module.
51 virtual bool runOnModule(Module &M) {
Chris Lattnerbe1987772005-12-22 06:07:52 +000052 CallGraph::initialize(M);
53
Chris Lattner00ca8d22006-01-14 20:03:00 +000054 ExternalCallingNode = getOrInsertFunction(0);
Chris Lattnerbe1987772005-12-22 06:07:52 +000055 CallsExternalNode = new CallGraphNode(0);
56 Root = 0;
57
Chris Lattner081375b2009-08-31 03:15:49 +000058 // Add every function to the call graph.
Chris Lattnerbe1987772005-12-22 06:07:52 +000059 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
60 addToCallGraph(I);
61
62 // If we didn't find a main function, use the external call graph node
63 if (Root == 0) Root = ExternalCallingNode;
64
65 return false;
Chris Lattnerbeed7422002-03-06 20:19:35 +000066 }
Misha Brukman01808ca2005-04-21 21:13:18 +000067
Chris Lattnerbe1987772005-12-22 06:07:52 +000068 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
69 AU.setPreservesAll();
70 }
Chris Lattnerbeed7422002-03-06 20:19:35 +000071
Chris Lattner13626022009-08-23 06:03:38 +000072 virtual void print(raw_ostream &OS, const Module *) const {
73 OS << "CallGraph Root is: ";
Chris Lattnerbe1987772005-12-22 06:07:52 +000074 if (Function *F = getRoot()->getFunction())
Chris Lattner13626022009-08-23 06:03:38 +000075 OS << F->getName() << "\n";
76 else {
77 OS << "<<null function: 0x" << getRoot() << ">>\n";
78 }
Chris Lattnerbe1987772005-12-22 06:07:52 +000079
Chris Lattner13626022009-08-23 06:03:38 +000080 CallGraph::print(OS, 0);
Chris Lattnerbe1987772005-12-22 06:07:52 +000081 }
82
83 virtual void releaseMemory() {
84 destroy();
85 }
86
Chris Lattner397af342010-01-20 19:51:46 +000087 /// getAdjustedAnalysisPointer - This method is used when a pass implements
88 /// an analysis interface through multiple inheritance. If needed, it should
89 /// override this to adjust the this pointer as needed for the specified pass
90 /// info.
Owen Andersona7aed182010-08-06 18:33:48 +000091 virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
92 if (PI == &CallGraph::ID)
Chris Lattner397af342010-01-20 19:51:46 +000093 return (CallGraph*)this;
94 return this;
95 }
96
Chris Lattnerbe1987772005-12-22 06:07:52 +000097 CallGraphNode* getExternalCallingNode() const { return ExternalCallingNode; }
98 CallGraphNode* getCallsExternalNode() const { return CallsExternalNode; }
99
100 // getRoot - Return the root of the call graph, which is either main, or if
101 // main cannot be found, the external node.
Chris Lattnerbeed7422002-03-06 20:19:35 +0000102 //
Chris Lattnerbe1987772005-12-22 06:07:52 +0000103 CallGraphNode *getRoot() { return Root; }
104 const CallGraphNode *getRoot() const { return Root; }
105
106private:
107 //===---------------------------------------------------------------------
108 // Implementation of CallGraph construction
109 //
Chris Lattnerbe1987772005-12-22 06:07:52 +0000110
Chris Lattnerbe1987772005-12-22 06:07:52 +0000111 // addToCallGraph - Add a function to the call graph, and link the node to all
112 // of the functions that it calls.
113 //
114 void addToCallGraph(Function *F) {
Chris Lattner00ca8d22006-01-14 20:03:00 +0000115 CallGraphNode *Node = getOrInsertFunction(F);
Chris Lattnerbe1987772005-12-22 06:07:52 +0000116
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000117 // If this function has external linkage, anything could call it.
Rafael Espindola6de96a12009-01-15 20:18:42 +0000118 if (!F->hasLocalLinkage()) {
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000119 ExternalCallingNode->addCalledFunction(CallSite(), Node);
Chris Lattnerbe1987772005-12-22 06:07:52 +0000120
121 // Found the entry point?
122 if (F->getName() == "main") {
123 if (Root) // Found multiple external mains? Don't pick one.
124 Root = ExternalCallingNode;
125 else
126 Root = Node; // Found a main, keep track of it!
127 }
128 }
129
Eli Friedmanf0bb0c22011-10-20 03:23:14 +0000130 // If this function has its address taken, anything could call it.
131 if (F->hasAddressTaken())
132 ExternalCallingNode->addCalledFunction(CallSite(), Node);
Duncan Sandse5579932008-09-09 19:56:34 +0000133
Chris Lattnerbe1987772005-12-22 06:07:52 +0000134 // If this function is not defined in this translation unit, it could call
135 // anything.
Duncan Sands38ef3a82007-12-03 20:06:50 +0000136 if (F->isDeclaration() && !F->isIntrinsic())
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000137 Node->addCalledFunction(CallSite(), CallsExternalNode);
Chris Lattnerbe1987772005-12-22 06:07:52 +0000138
Duncan Sandsc189e792008-09-09 12:40:47 +0000139 // Look for calls by this function.
Chris Lattnerbe1987772005-12-22 06:07:52 +0000140 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
141 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
142 II != IE; ++II) {
Gabor Greif67a970b2010-07-28 12:19:46 +0000143 CallSite CS(cast<Value>(II));
Duncan Sands8598a0e2012-09-26 17:16:01 +0000144 if (CS) {
Duncan Sandsc189e792008-09-09 12:40:47 +0000145 const Function *Callee = CS.getCalledFunction();
Duncan Sands8598a0e2012-09-26 17:16:01 +0000146 if (!Callee)
147 // Indirect calls of intrinsics are not allowed so no need to check.
Duncan Sandsc189e792008-09-09 12:40:47 +0000148 Node->addCalledFunction(CS, CallsExternalNode);
Duncan Sands8598a0e2012-09-26 17:16:01 +0000149 else if (!Callee->isIntrinsic())
150 Node->addCalledFunction(CS, getOrInsertFunction(Callee));
Duncan Sandsc189e792008-09-09 12:40:47 +0000151 }
Chris Lattnerbe1987772005-12-22 06:07:52 +0000152 }
153 }
154
155 //
156 // destroy - Release memory for the call graph
157 virtual void destroy() {
Chris Lattnerdd23d3d2006-12-05 19:46:12 +0000158 /// CallsExternalNode is not in the function map, delete it explicitly.
Benjamin Kramer39585772010-04-20 12:16:50 +0000159 if (CallsExternalNode) {
160 CallsExternalNode->allReferencesDropped();
161 delete CallsExternalNode;
162 CallsExternalNode = 0;
163 }
Chris Lattnerdd23d3d2006-12-05 19:46:12 +0000164 CallGraph::destroy();
Chris Lattnerbe1987772005-12-22 06:07:52 +0000165 }
166};
Chris Lattnerbbf3ae82001-09-28 00:08:15 +0000167
Chris Lattnerbe1987772005-12-22 06:07:52 +0000168} //End anonymous namespace
169
Owen Andersonc266a362010-10-13 21:49:58 +0000170INITIALIZE_ANALYSIS_GROUP(CallGraph, "Call Graph", BasicCallGraph)
Owen Andersonac4a1ed2010-07-21 23:07:00 +0000171INITIALIZE_AG_PASS(BasicCallGraph, CallGraph, "basiccg",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000172 "Basic CallGraph Construction", false, true, true)
Dan Gohmand78c4002008-05-13 00:00:25 +0000173
Devang Patel8c78a0b2007-05-03 01:11:54 +0000174char CallGraph::ID = 0;
175char BasicCallGraph::ID = 0;
Lauro Ramos Venancio41223582007-05-02 20:37:47 +0000176
Chris Lattnerbe1987772005-12-22 06:07:52 +0000177void CallGraph::initialize(Module &M) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000178 Mod = &M;
Chris Lattnerbbf3ae82001-09-28 00:08:15 +0000179}
180
Chris Lattner80327322002-03-06 17:16:43 +0000181void CallGraph::destroy() {
Chris Lattner081375b2009-08-31 03:15:49 +0000182 if (FunctionMap.empty()) return;
183
Chris Lattneraedb8a32010-04-20 00:47:34 +0000184 // Reset all node's use counts to zero before deleting them to prevent an
185 // assertion from firing.
186#ifndef NDEBUG
187 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
188 I != E; ++I)
189 I->second->allReferencesDropped();
190#endif
191
Chris Lattner081375b2009-08-31 03:15:49 +0000192 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
193 I != E; ++I)
194 delete I->second;
195 FunctionMap.clear();
Chris Lattner03946cd2001-11-26 18:51:25 +0000196}
197
Chris Lattner13626022009-08-23 06:03:38 +0000198void CallGraph::print(raw_ostream &OS, Module*) const {
Chris Lattnerb9d55472002-11-04 00:21:19 +0000199 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner6b5411022004-08-08 03:27:49 +0000200 I->second->print(OS);
201}
Manman Ren49d684e2012-09-12 05:06:18 +0000202#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Chris Lattner69349a52009-08-30 22:24:32 +0000203void CallGraph::dump() const {
David Greeneba44b3e2009-12-23 20:03:58 +0000204 print(dbgs(), 0);
Chris Lattner69349a52009-08-30 22:24:32 +0000205}
Manman Renc3366cc2012-09-06 19:55:56 +0000206#endif
Chris Lattner6b5411022004-08-08 03:27:49 +0000207
Chris Lattner03946cd2001-11-26 18:51:25 +0000208//===----------------------------------------------------------------------===//
209// Implementations of public modification methods
210//
211
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000212// removeFunctionFromModule - Unlink the function from this module, returning
213// it. Because this removes the function from the module, the call graph node
214// is destroyed. This is only valid if the function does not call any other
215// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner03946cd2001-11-26 18:51:25 +0000216// is to dropAllReferences before calling this.
217//
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000218Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
Chris Lattner4275cf22009-08-31 02:24:20 +0000219 assert(CGN->empty() && "Cannot remove function from call "
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000220 "graph if it references other functions!");
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000221 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000222 delete CGN; // Delete the call graph node for this func
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000223 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner03946cd2001-11-26 18:51:25 +0000224
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000225 Mod->getFunctionList().remove(F);
226 return F;
Chris Lattner03946cd2001-11-26 18:51:25 +0000227}
228
Nick Lewycky0f87ca72011-01-03 03:19:35 +0000229/// spliceFunction - Replace the function represented by this node by another.
230/// This does not rescan the body of the function, so it is suitable when
231/// splicing the body of the old function to the new while also updating all
232/// callers from old to new.
233///
234void CallGraph::spliceFunction(const Function *From, const Function *To) {
235 assert(FunctionMap.count(From) && "No CallGraphNode for function!");
236 assert(!FunctionMap.count(To) &&
237 "Pointing CallGraphNode at a function that already exists");
238 FunctionMapTy::iterator I = FunctionMap.find(From);
239 I->second->F = const_cast<Function*>(To);
240 FunctionMap[To] = I->second;
241 FunctionMap.erase(I);
242}
243
Chris Lattner00ca8d22006-01-14 20:03:00 +0000244// getOrInsertFunction - This method is identical to calling operator[], but
245// it will insert a new CallGraphNode for the specified function if one does
246// not already exist.
247CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
248 CallGraphNode *&CGN = FunctionMap[F];
249 if (CGN) return CGN;
250
251 assert((!F || F->getParent() == Mod) && "Function not in current module!");
252 return CGN = new CallGraphNode(const_cast<Function*>(F));
253}
254
Chris Lattner13626022009-08-23 06:03:38 +0000255void CallGraphNode::print(raw_ostream &OS) const {
Chris Lattnerbe1987772005-12-22 06:07:52 +0000256 if (Function *F = getFunction())
Chris Lattner081375b2009-08-31 03:15:49 +0000257 OS << "Call graph node for function: '" << F->getName() << "'";
Chris Lattnerbe1987772005-12-22 06:07:52 +0000258 else
Chris Lattner081375b2009-08-31 03:15:49 +0000259 OS << "Call graph node <<null function>>";
260
Chris Lattner8c562542010-04-23 18:23:40 +0000261 OS << "<<" << this << ">> #uses=" << getNumReferences() << '\n';
Chris Lattnerbe1987772005-12-22 06:07:52 +0000262
Chris Lattner8c562542010-04-23 18:23:40 +0000263 for (const_iterator I = begin(), E = end(); I != E; ++I) {
264 OS << " CS<" << I->first << "> calls ";
Gabor Greif191812f2009-01-14 17:09:04 +0000265 if (Function *FI = I->second->getFunction())
Chris Lattner8c562542010-04-23 18:23:40 +0000266 OS << "function '" << FI->getName() <<"'\n";
267 else
268 OS << "external node\n";
269 }
270 OS << '\n';
Chris Lattnerbe1987772005-12-22 06:07:52 +0000271}
272
Manman Ren49d684e2012-09-12 05:06:18 +0000273#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
David Greeneba44b3e2009-12-23 20:03:58 +0000274void CallGraphNode::dump() const { print(dbgs()); }
Manman Renc3366cc2012-09-06 19:55:56 +0000275#endif
Chris Lattnerbe1987772005-12-22 06:07:52 +0000276
Chris Lattnercc9709c2008-04-13 19:41:25 +0000277/// removeCallEdgeFor - This method removes the edge in the node for the
278/// specified call site. Note that this method takes linear time, so it
279/// should be used sparingly.
280void CallGraphNode::removeCallEdgeFor(CallSite CS) {
Chris Lattnereedcd842009-08-31 07:23:46 +0000281 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
282 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
Chris Lattner063d0652009-09-01 06:31:31 +0000283 if (I->first == CS.getInstruction()) {
Chris Lattnereedcd842009-08-31 07:23:46 +0000284 I->second->DropRef();
285 *I = CalledFunctions.back();
286 CalledFunctions.pop_back();
287 return;
288 }
Chris Lattnercc9709c2008-04-13 19:41:25 +0000289 }
290}
291
Chris Lattner824a2182004-09-18 21:34:34 +0000292// removeAnyCallEdgeTo - This method removes any call edges from this node to
293// the specified callee function. This takes more time to execute than
294// removeCallEdgeTo, so it should not be used unless necessary.
295void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
Chris Lattnerd6d99df2004-09-19 19:01:06 +0000296 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000297 if (CalledFunctions[i].second == Callee) {
Chris Lattner081375b2009-08-31 03:15:49 +0000298 Callee->DropRef();
Chris Lattnerd6d99df2004-09-19 19:01:06 +0000299 CalledFunctions[i] = CalledFunctions.back();
300 CalledFunctions.pop_back();
301 --i; --e;
Chris Lattner824a2182004-09-18 21:34:34 +0000302 }
303}
Reid Spencerbe535662006-06-07 22:00:26 +0000304
Duncan Sands3a813a52008-10-03 07:36:09 +0000305/// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite
306/// from this node to the specified callee function.
307void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) {
Gabor Greif20b722f2009-01-17 19:46:01 +0000308 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
309 assert(I != CalledFunctions.end() && "Cannot find callee to remove!");
310 CallRecord &CR = *I;
Chris Lattner063d0652009-09-01 06:31:31 +0000311 if (CR.second == Callee && CR.first == 0) {
Chris Lattner081375b2009-08-31 03:15:49 +0000312 Callee->DropRef();
313 *I = CalledFunctions.back();
314 CalledFunctions.pop_back();
Duncan Sands3a813a52008-10-03 07:36:09 +0000315 return;
316 }
317 }
318}
319
Chris Lattnere0987212009-09-15 05:40:35 +0000320/// replaceCallEdge - This method replaces the edge in the node for the
321/// specified call site with a new one. Note that this method takes linear
322/// time, so it should be used sparingly.
323void CallGraphNode::replaceCallEdge(CallSite CS,
324 CallSite NewCS, CallGraphNode *NewNode){
325 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
326 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
327 if (I->first == CS.getInstruction()) {
328 I->second->DropRef();
329 I->first = NewCS.getInstruction();
330 I->second = NewNode;
331 NewNode->AddRef();
332 return;
333 }
334 }
335}
336
Reid Spencerbe535662006-06-07 22:00:26 +0000337// Enuse that users of CallGraph.h also link with this file
338DEFINING_FILE_FOR(CallGraph)