blob: 690c4b4b6f1a63426283e3d43d79837f91dbb6f8 [file] [log] [blame]
Chris Lattner8d5a16c2002-03-06 18:00:49 +00001//===- CallGraph.cpp - Build a Module's call graph ------------------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner41fbf302001-09-28 00:08:15 +00009//
Chris Lattner03839952005-12-22 06:07:52 +000010// This file implements the CallGraph class and provides the BasicCallGraph
11// default implementation.
Chris Lattner9f9e2be2001-10-13 06:33:19 +000012//
Chris Lattner41fbf302001-09-28 00:08:15 +000013//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/CallGraph.h"
Chris Lattner41fbf302001-09-28 00:08:15 +000016#include "llvm/Module.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000017#include "llvm/Instructions.h"
Dale Johannesen1f67ce42009-03-19 18:03:56 +000018#include "llvm/IntrinsicInst.h"
Chris Lattner07a38e72003-10-31 21:05:12 +000019#include "llvm/Support/CallSite.h"
David Greene265c0262009-12-23 20:03:58 +000020#include "llvm/Support/Debug.h"
Chris Lattner45cfe542009-08-23 06:03:38 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattnerb81c0212004-04-12 05:36:32 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattner03839952005-12-22 06:07:52 +000024namespace {
25
26//===----------------------------------------------------------------------===//
27// BasicCallGraph class definition
Chris Lattner41fbf302001-09-28 00:08:15 +000028//
Chris Lattnercdac3a32010-01-20 19:51:46 +000029class BasicCallGraph : public ModulePass, public CallGraph {
Chris Lattner03839952005-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 Lattner41fbf302001-09-28 00:08:15 +000034
Chris Lattner03839952005-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 Lattner25e9cad2001-11-26 18:51:25 +000038
Chris Lattner03839952005-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 Patel19974732007-05-03 01:11:54 +000044 static char ID; // Class identification, replacement for typeinfo
Owen Anderson90c579d2010-08-06 18:33:48 +000045 BasicCallGraph() : ModulePass(ID), Root(0),
Owen Anderson081c34b2010-10-19 17:21:58 +000046 ExternalCallingNode(0), CallsExternalNode(0) {
47 initializeBasicCallGraphPass(*PassRegistry::getPassRegistry());
48 }
Chris Lattner03839952005-12-22 06:07:52 +000049
50 // runOnModule - Compute the call graph for the specified module.
51 virtual bool runOnModule(Module &M) {
Chris Lattner03839952005-12-22 06:07:52 +000052 CallGraph::initialize(M);
53
Chris Lattnerc54b1c12006-01-14 20:03:00 +000054 ExternalCallingNode = getOrInsertFunction(0);
Chris Lattner03839952005-12-22 06:07:52 +000055 CallsExternalNode = new CallGraphNode(0);
56 Root = 0;
57
Chris Lattnerb374b902009-08-31 03:15:49 +000058 // Add every function to the call graph.
Chris Lattner03839952005-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 Lattnerd4d427b2002-03-06 20:19:35 +000066 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000067
Chris Lattner03839952005-12-22 06:07:52 +000068 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
69 AU.setPreservesAll();
70 }
Chris Lattnerd4d427b2002-03-06 20:19:35 +000071
Chris Lattner45cfe542009-08-23 06:03:38 +000072 virtual void print(raw_ostream &OS, const Module *) const {
73 OS << "CallGraph Root is: ";
Chris Lattner03839952005-12-22 06:07:52 +000074 if (Function *F = getRoot()->getFunction())
Chris Lattner45cfe542009-08-23 06:03:38 +000075 OS << F->getName() << "\n";
76 else {
77 OS << "<<null function: 0x" << getRoot() << ">>\n";
78 }
Chris Lattner03839952005-12-22 06:07:52 +000079
Chris Lattner45cfe542009-08-23 06:03:38 +000080 CallGraph::print(OS, 0);
Chris Lattner03839952005-12-22 06:07:52 +000081 }
82
83 virtual void releaseMemory() {
84 destroy();
85 }
86
Chris Lattnercdac3a32010-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 Anderson90c579d2010-08-06 18:33:48 +000091 virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
92 if (PI == &CallGraph::ID)
Chris Lattnercdac3a32010-01-20 19:51:46 +000093 return (CallGraph*)this;
94 return this;
95 }
96
Chris Lattner03839952005-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 Lattnerd4d427b2002-03-06 20:19:35 +0000102 //
Chris Lattner03839952005-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 Lattner03839952005-12-22 06:07:52 +0000110
Chris Lattner03839952005-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 Lattnerc54b1c12006-01-14 20:03:00 +0000115 CallGraphNode *Node = getOrInsertFunction(F);
Chris Lattner03839952005-12-22 06:07:52 +0000116
Chris Lattnerd85340f2006-07-12 18:29:36 +0000117 // If this function has external linkage, anything could call it.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000118 if (!F->hasLocalLinkage()) {
Chris Lattnerd85340f2006-07-12 18:29:36 +0000119 ExternalCallingNode->addCalledFunction(CallSite(), Node);
Chris Lattner03839952005-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
Duncan Sands7ca9d812008-09-09 19:56:34 +0000130 // Loop over all of the users of the function, looking for non-call uses.
Gabor Greif517e1242010-07-09 13:17:13 +0000131 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I){
132 User *U = *I;
133 if ((!isa<CallInst>(U) && !isa<InvokeInst>(U))
134 || !CallSite(cast<Instruction>(U)).isCallee(I)) {
Duncan Sands7ca9d812008-09-09 19:56:34 +0000135 // Not a call, or being used as a parameter rather than as the callee.
136 ExternalCallingNode->addCalledFunction(CallSite(), Node);
137 break;
138 }
Gabor Greif517e1242010-07-09 13:17:13 +0000139 }
Duncan Sands7ca9d812008-09-09 19:56:34 +0000140
Chris Lattner03839952005-12-22 06:07:52 +0000141 // If this function is not defined in this translation unit, it could call
142 // anything.
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000143 if (F->isDeclaration() && !F->isIntrinsic())
Chris Lattnerd85340f2006-07-12 18:29:36 +0000144 Node->addCalledFunction(CallSite(), CallsExternalNode);
Chris Lattner03839952005-12-22 06:07:52 +0000145
Duncan Sands99c1a7c2008-09-09 12:40:47 +0000146 // Look for calls by this function.
Chris Lattner03839952005-12-22 06:07:52 +0000147 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
148 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
149 II != IE; ++II) {
Gabor Greif37379842010-07-28 12:19:46 +0000150 CallSite CS(cast<Value>(II));
151 if (CS && !isa<DbgInfoIntrinsic>(II)) {
Duncan Sands99c1a7c2008-09-09 12:40:47 +0000152 const Function *Callee = CS.getCalledFunction();
153 if (Callee)
154 Node->addCalledFunction(CS, getOrInsertFunction(Callee));
155 else
156 Node->addCalledFunction(CS, CallsExternalNode);
157 }
Chris Lattner03839952005-12-22 06:07:52 +0000158 }
159 }
160
161 //
162 // destroy - Release memory for the call graph
163 virtual void destroy() {
Chris Lattnerf2d9ceb2006-12-05 19:46:12 +0000164 /// CallsExternalNode is not in the function map, delete it explicitly.
Benjamin Kramereb1f4b12010-04-20 12:16:50 +0000165 if (CallsExternalNode) {
166 CallsExternalNode->allReferencesDropped();
167 delete CallsExternalNode;
168 CallsExternalNode = 0;
169 }
Chris Lattnerf2d9ceb2006-12-05 19:46:12 +0000170 CallGraph::destroy();
Chris Lattner03839952005-12-22 06:07:52 +0000171 }
172};
Chris Lattner41fbf302001-09-28 00:08:15 +0000173
Chris Lattner03839952005-12-22 06:07:52 +0000174} //End anonymous namespace
175
Owen Anderson325e2642010-10-13 21:49:58 +0000176INITIALIZE_ANALYSIS_GROUP(CallGraph, "Call Graph", BasicCallGraph)
Owen Andersond8cc7be2010-07-21 23:07:00 +0000177INITIALIZE_AG_PASS(BasicCallGraph, CallGraph, "basiccg",
Owen Andersonce665bd2010-10-07 22:25:06 +0000178 "Basic CallGraph Construction", false, true, true)
Dan Gohman844731a2008-05-13 00:00:25 +0000179
Devang Patel19974732007-05-03 01:11:54 +0000180char CallGraph::ID = 0;
181char BasicCallGraph::ID = 0;
Lauro Ramos Venancioc7182882007-05-02 20:37:47 +0000182
Chris Lattner03839952005-12-22 06:07:52 +0000183void CallGraph::initialize(Module &M) {
Chris Lattner7e708292002-06-25 16:13:24 +0000184 Mod = &M;
Chris Lattner41fbf302001-09-28 00:08:15 +0000185}
186
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000187void CallGraph::destroy() {
Chris Lattnerb374b902009-08-31 03:15:49 +0000188 if (FunctionMap.empty()) return;
189
Chris Lattner8a39ed72010-04-20 00:47:34 +0000190 // Reset all node's use counts to zero before deleting them to prevent an
191 // assertion from firing.
192#ifndef NDEBUG
193 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
194 I != E; ++I)
195 I->second->allReferencesDropped();
196#endif
197
Chris Lattnerb374b902009-08-31 03:15:49 +0000198 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
199 I != E; ++I)
200 delete I->second;
201 FunctionMap.clear();
Chris Lattner25e9cad2001-11-26 18:51:25 +0000202}
203
Chris Lattner45cfe542009-08-23 06:03:38 +0000204void CallGraph::print(raw_ostream &OS, Module*) const {
Chris Lattner16500152002-11-04 00:21:19 +0000205 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattneraf8a4242004-08-08 03:27:49 +0000206 I->second->print(OS);
207}
Chris Lattner23603a62009-08-30 22:24:32 +0000208void CallGraph::dump() const {
David Greene265c0262009-12-23 20:03:58 +0000209 print(dbgs(), 0);
Chris Lattner23603a62009-08-30 22:24:32 +0000210}
Chris Lattneraf8a4242004-08-08 03:27:49 +0000211
Chris Lattner25e9cad2001-11-26 18:51:25 +0000212//===----------------------------------------------------------------------===//
213// Implementations of public modification methods
214//
215
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000216// removeFunctionFromModule - Unlink the function from this module, returning
217// it. Because this removes the function from the module, the call graph node
218// is destroyed. This is only valid if the function does not call any other
219// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner25e9cad2001-11-26 18:51:25 +0000220// is to dropAllReferences before calling this.
221//
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000222Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
Chris Lattner2adb8302009-08-31 02:24:20 +0000223 assert(CGN->empty() && "Cannot remove function from call "
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000224 "graph if it references other functions!");
Chris Lattner5714c972003-08-31 20:36:52 +0000225 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000226 delete CGN; // Delete the call graph node for this func
Chris Lattner5714c972003-08-31 20:36:52 +0000227 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner25e9cad2001-11-26 18:51:25 +0000228
Chris Lattner5714c972003-08-31 20:36:52 +0000229 Mod->getFunctionList().remove(F);
230 return F;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000231}
232
Nick Lewycky9ad1cb52011-01-03 03:19:35 +0000233/// spliceFunction - Replace the function represented by this node by another.
234/// This does not rescan the body of the function, so it is suitable when
235/// splicing the body of the old function to the new while also updating all
236/// callers from old to new.
237///
238void CallGraph::spliceFunction(const Function *From, const Function *To) {
239 assert(FunctionMap.count(From) && "No CallGraphNode for function!");
240 assert(!FunctionMap.count(To) &&
241 "Pointing CallGraphNode at a function that already exists");
242 FunctionMapTy::iterator I = FunctionMap.find(From);
243 I->second->F = const_cast<Function*>(To);
244 FunctionMap[To] = I->second;
245 FunctionMap.erase(I);
246}
247
Chris Lattnerc54b1c12006-01-14 20:03:00 +0000248// getOrInsertFunction - This method is identical to calling operator[], but
249// it will insert a new CallGraphNode for the specified function if one does
250// not already exist.
251CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
252 CallGraphNode *&CGN = FunctionMap[F];
253 if (CGN) return CGN;
254
255 assert((!F || F->getParent() == Mod) && "Function not in current module!");
256 return CGN = new CallGraphNode(const_cast<Function*>(F));
257}
258
Chris Lattner45cfe542009-08-23 06:03:38 +0000259void CallGraphNode::print(raw_ostream &OS) const {
Chris Lattner03839952005-12-22 06:07:52 +0000260 if (Function *F = getFunction())
Chris Lattnerb374b902009-08-31 03:15:49 +0000261 OS << "Call graph node for function: '" << F->getName() << "'";
Chris Lattner03839952005-12-22 06:07:52 +0000262 else
Chris Lattnerb374b902009-08-31 03:15:49 +0000263 OS << "Call graph node <<null function>>";
264
Chris Lattner62754132010-04-23 18:23:40 +0000265 OS << "<<" << this << ">> #uses=" << getNumReferences() << '\n';
Chris Lattner03839952005-12-22 06:07:52 +0000266
Chris Lattner62754132010-04-23 18:23:40 +0000267 for (const_iterator I = begin(), E = end(); I != E; ++I) {
268 OS << " CS<" << I->first << "> calls ";
Gabor Greif61046262009-01-14 17:09:04 +0000269 if (Function *FI = I->second->getFunction())
Chris Lattner62754132010-04-23 18:23:40 +0000270 OS << "function '" << FI->getName() <<"'\n";
271 else
272 OS << "external node\n";
273 }
274 OS << '\n';
Chris Lattner03839952005-12-22 06:07:52 +0000275}
276
David Greene265c0262009-12-23 20:03:58 +0000277void CallGraphNode::dump() const { print(dbgs()); }
Chris Lattner03839952005-12-22 06:07:52 +0000278
Chris Lattner75caee22008-04-13 19:41:25 +0000279/// removeCallEdgeFor - This method removes the edge in the node for the
280/// specified call site. Note that this method takes linear time, so it
281/// should be used sparingly.
282void CallGraphNode::removeCallEdgeFor(CallSite CS) {
Chris Lattnerbe577652009-08-31 07:23:46 +0000283 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
284 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
Chris Lattnera541b0f2009-09-01 06:31:31 +0000285 if (I->first == CS.getInstruction()) {
Chris Lattnerbe577652009-08-31 07:23:46 +0000286 I->second->DropRef();
287 *I = CalledFunctions.back();
288 CalledFunctions.pop_back();
289 return;
290 }
Chris Lattner75caee22008-04-13 19:41:25 +0000291 }
292}
293
Chris Lattnercd382a32004-09-18 21:34:34 +0000294// removeAnyCallEdgeTo - This method removes any call edges from this node to
295// the specified callee function. This takes more time to execute than
296// removeCallEdgeTo, so it should not be used unless necessary.
297void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
Chris Lattnerfff03c92004-09-19 19:01:06 +0000298 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
Chris Lattnerd85340f2006-07-12 18:29:36 +0000299 if (CalledFunctions[i].second == Callee) {
Chris Lattnerb374b902009-08-31 03:15:49 +0000300 Callee->DropRef();
Chris Lattnerfff03c92004-09-19 19:01:06 +0000301 CalledFunctions[i] = CalledFunctions.back();
302 CalledFunctions.pop_back();
303 --i; --e;
Chris Lattnercd382a32004-09-18 21:34:34 +0000304 }
305}
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000306
Duncan Sandsa2582da2008-10-03 07:36:09 +0000307/// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite
308/// from this node to the specified callee function.
309void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) {
Gabor Greif7f2e3812009-01-17 19:46:01 +0000310 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
311 assert(I != CalledFunctions.end() && "Cannot find callee to remove!");
312 CallRecord &CR = *I;
Chris Lattnera541b0f2009-09-01 06:31:31 +0000313 if (CR.second == Callee && CR.first == 0) {
Chris Lattnerb374b902009-08-31 03:15:49 +0000314 Callee->DropRef();
315 *I = CalledFunctions.back();
316 CalledFunctions.pop_back();
Duncan Sandsa2582da2008-10-03 07:36:09 +0000317 return;
318 }
319 }
320}
321
Chris Lattnera51c39c2009-09-15 05:40:35 +0000322/// replaceCallEdge - This method replaces the edge in the node for the
323/// specified call site with a new one. Note that this method takes linear
324/// time, so it should be used sparingly.
325void CallGraphNode::replaceCallEdge(CallSite CS,
326 CallSite NewCS, CallGraphNode *NewNode){
327 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
328 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
329 if (I->first == CS.getInstruction()) {
330 I->second->DropRef();
331 I->first = NewCS.getInstruction();
332 I->second = NewNode;
333 NewNode->AddRef();
334 return;
335 }
336 }
337}
338
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000339// Enuse that users of CallGraph.h also link with this file
340DEFINING_FILE_FOR(CallGraph)