blob: 1af0055ce541151460e43daf4e5c428d59e12055 [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 Anderson1f745902010-08-06 00:23:35 +000045 BasicCallGraph() : ModulePass(&ID), Root(0),
Devang Patel794fd752007-05-01 21:15:47 +000046 ExternalCallingNode(0), CallsExternalNode(0) {}
Chris Lattner03839952005-12-22 06:07:52 +000047
48 // runOnModule - Compute the call graph for the specified module.
49 virtual bool runOnModule(Module &M) {
Chris Lattner03839952005-12-22 06:07:52 +000050 CallGraph::initialize(M);
51
Chris Lattnerc54b1c12006-01-14 20:03:00 +000052 ExternalCallingNode = getOrInsertFunction(0);
Chris Lattner03839952005-12-22 06:07:52 +000053 CallsExternalNode = new CallGraphNode(0);
54 Root = 0;
55
Chris Lattnerb374b902009-08-31 03:15:49 +000056 // Add every function to the call graph.
Chris Lattner03839952005-12-22 06:07:52 +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;
Chris Lattnerd4d427b2002-03-06 20:19:35 +000064 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000065
Chris Lattner03839952005-12-22 06:07:52 +000066 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
67 AU.setPreservesAll();
68 }
Chris Lattnerd4d427b2002-03-06 20:19:35 +000069
Chris Lattner45cfe542009-08-23 06:03:38 +000070 virtual void print(raw_ostream &OS, const Module *) const {
71 OS << "CallGraph Root is: ";
Chris Lattner03839952005-12-22 06:07:52 +000072 if (Function *F = getRoot()->getFunction())
Chris Lattner45cfe542009-08-23 06:03:38 +000073 OS << F->getName() << "\n";
74 else {
75 OS << "<<null function: 0x" << getRoot() << ">>\n";
76 }
Chris Lattner03839952005-12-22 06:07:52 +000077
Chris Lattner45cfe542009-08-23 06:03:38 +000078 CallGraph::print(OS, 0);
Chris Lattner03839952005-12-22 06:07:52 +000079 }
80
81 virtual void releaseMemory() {
82 destroy();
83 }
84
Chris Lattnercdac3a32010-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 Anderson1f745902010-08-06 00:23:35 +000089 virtual void *getAdjustedAnalysisPointer(const PassInfo *PI) {
90 if (PI->isPassID(&CallGraph::ID))
Chris Lattnercdac3a32010-01-20 19:51:46 +000091 return (CallGraph*)this;
92 return this;
93 }
94
Chris Lattner03839952005-12-22 06:07:52 +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.
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000100 //
Chris Lattner03839952005-12-22 06:07:52 +0000101 CallGraphNode *getRoot() { return Root; }
102 const CallGraphNode *getRoot() const { return Root; }
103
104private:
105 //===---------------------------------------------------------------------
106 // Implementation of CallGraph construction
107 //
Chris Lattner03839952005-12-22 06:07:52 +0000108
Chris Lattner03839952005-12-22 06:07:52 +0000109 // 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) {
Chris Lattnerc54b1c12006-01-14 20:03:00 +0000113 CallGraphNode *Node = getOrInsertFunction(F);
Chris Lattner03839952005-12-22 06:07:52 +0000114
Chris Lattnerd85340f2006-07-12 18:29:36 +0000115 // If this function has external linkage, anything could call it.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000116 if (!F->hasLocalLinkage()) {
Chris Lattnerd85340f2006-07-12 18:29:36 +0000117 ExternalCallingNode->addCalledFunction(CallSite(), Node);
Chris Lattner03839952005-12-22 06:07:52 +0000118
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 Sands7ca9d812008-09-09 19:56:34 +0000128 // Loop over all of the users of the function, looking for non-call uses.
Gabor Greif517e1242010-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 Sands7ca9d812008-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 Greif517e1242010-07-09 13:17:13 +0000137 }
Duncan Sands7ca9d812008-09-09 19:56:34 +0000138
Chris Lattner03839952005-12-22 06:07:52 +0000139 // If this function is not defined in this translation unit, it could call
140 // anything.
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000141 if (F->isDeclaration() && !F->isIntrinsic())
Chris Lattnerd85340f2006-07-12 18:29:36 +0000142 Node->addCalledFunction(CallSite(), CallsExternalNode);
Chris Lattner03839952005-12-22 06:07:52 +0000143
Duncan Sands99c1a7c2008-09-09 12:40:47 +0000144 // Look for calls by this function.
Chris Lattner03839952005-12-22 06:07:52 +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) {
Gabor Greif37379842010-07-28 12:19:46 +0000148 CallSite CS(cast<Value>(II));
149 if (CS && !isa<DbgInfoIntrinsic>(II)) {
Duncan Sands99c1a7c2008-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 }
Chris Lattner03839952005-12-22 06:07:52 +0000156 }
157 }
158
159 //
160 // destroy - Release memory for the call graph
161 virtual void destroy() {
Chris Lattnerf2d9ceb2006-12-05 19:46:12 +0000162 /// CallsExternalNode is not in the function map, delete it explicitly.
Benjamin Kramereb1f4b12010-04-20 12:16:50 +0000163 if (CallsExternalNode) {
164 CallsExternalNode->allReferencesDropped();
165 delete CallsExternalNode;
166 CallsExternalNode = 0;
167 }
Chris Lattnerf2d9ceb2006-12-05 19:46:12 +0000168 CallGraph::destroy();
Chris Lattner03839952005-12-22 06:07:52 +0000169 }
170};
Chris Lattner41fbf302001-09-28 00:08:15 +0000171
Chris Lattner03839952005-12-22 06:07:52 +0000172} //End anonymous namespace
173
Dan Gohman844731a2008-05-13 00:00:25 +0000174static RegisterAnalysisGroup<CallGraph> X("Call Graph");
Owen Andersond8cc7be2010-07-21 23:07:00 +0000175INITIALIZE_AG_PASS(BasicCallGraph, CallGraph, "basiccg",
176 "Basic CallGraph Construction", false, true, true);
Dan Gohman844731a2008-05-13 00:00:25 +0000177
Devang Patel19974732007-05-03 01:11:54 +0000178char CallGraph::ID = 0;
179char BasicCallGraph::ID = 0;
Lauro Ramos Venancioc7182882007-05-02 20:37:47 +0000180
Chris Lattner03839952005-12-22 06:07:52 +0000181void CallGraph::initialize(Module &M) {
Chris Lattner7e708292002-06-25 16:13:24 +0000182 Mod = &M;
Chris Lattner41fbf302001-09-28 00:08:15 +0000183}
184
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000185void CallGraph::destroy() {
Chris Lattnerb374b902009-08-31 03:15:49 +0000186 if (FunctionMap.empty()) return;
187
Chris Lattner8a39ed72010-04-20 00:47:34 +0000188 // Reset all node's use counts to zero before deleting them to prevent an
189 // assertion from firing.
190#ifndef NDEBUG
191 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
192 I != E; ++I)
193 I->second->allReferencesDropped();
194#endif
195
Chris Lattnerb374b902009-08-31 03:15:49 +0000196 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
197 I != E; ++I)
198 delete I->second;
199 FunctionMap.clear();
Chris Lattner25e9cad2001-11-26 18:51:25 +0000200}
201
Chris Lattner45cfe542009-08-23 06:03:38 +0000202void CallGraph::print(raw_ostream &OS, Module*) const {
Chris Lattner16500152002-11-04 00:21:19 +0000203 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattneraf8a4242004-08-08 03:27:49 +0000204 I->second->print(OS);
205}
Chris Lattner23603a62009-08-30 22:24:32 +0000206void CallGraph::dump() const {
David Greene265c0262009-12-23 20:03:58 +0000207 print(dbgs(), 0);
Chris Lattner23603a62009-08-30 22:24:32 +0000208}
Chris Lattneraf8a4242004-08-08 03:27:49 +0000209
Chris Lattner25e9cad2001-11-26 18:51:25 +0000210//===----------------------------------------------------------------------===//
211// Implementations of public modification methods
212//
213
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000214// removeFunctionFromModule - Unlink the function from this module, returning
215// it. Because this removes the function from the module, the call graph node
216// is destroyed. This is only valid if the function does not call any other
217// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner25e9cad2001-11-26 18:51:25 +0000218// is to dropAllReferences before calling this.
219//
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000220Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
Chris Lattner2adb8302009-08-31 02:24:20 +0000221 assert(CGN->empty() && "Cannot remove function from call "
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000222 "graph if it references other functions!");
Chris Lattner5714c972003-08-31 20:36:52 +0000223 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000224 delete CGN; // Delete the call graph node for this func
Chris Lattner5714c972003-08-31 20:36:52 +0000225 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner25e9cad2001-11-26 18:51:25 +0000226
Chris Lattner5714c972003-08-31 20:36:52 +0000227 Mod->getFunctionList().remove(F);
228 return F;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000229}
230
Chris Lattnerc54b1c12006-01-14 20:03:00 +0000231// getOrInsertFunction - This method is identical to calling operator[], but
232// it will insert a new CallGraphNode for the specified function if one does
233// not already exist.
234CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
235 CallGraphNode *&CGN = FunctionMap[F];
236 if (CGN) return CGN;
237
238 assert((!F || F->getParent() == Mod) && "Function not in current module!");
239 return CGN = new CallGraphNode(const_cast<Function*>(F));
240}
241
Chris Lattner45cfe542009-08-23 06:03:38 +0000242void CallGraphNode::print(raw_ostream &OS) const {
Chris Lattner03839952005-12-22 06:07:52 +0000243 if (Function *F = getFunction())
Chris Lattnerb374b902009-08-31 03:15:49 +0000244 OS << "Call graph node for function: '" << F->getName() << "'";
Chris Lattner03839952005-12-22 06:07:52 +0000245 else
Chris Lattnerb374b902009-08-31 03:15:49 +0000246 OS << "Call graph node <<null function>>";
247
Chris Lattner62754132010-04-23 18:23:40 +0000248 OS << "<<" << this << ">> #uses=" << getNumReferences() << '\n';
Chris Lattner03839952005-12-22 06:07:52 +0000249
Chris Lattner62754132010-04-23 18:23:40 +0000250 for (const_iterator I = begin(), E = end(); I != E; ++I) {
251 OS << " CS<" << I->first << "> calls ";
Gabor Greif61046262009-01-14 17:09:04 +0000252 if (Function *FI = I->second->getFunction())
Chris Lattner62754132010-04-23 18:23:40 +0000253 OS << "function '" << FI->getName() <<"'\n";
254 else
255 OS << "external node\n";
256 }
257 OS << '\n';
Chris Lattner03839952005-12-22 06:07:52 +0000258}
259
David Greene265c0262009-12-23 20:03:58 +0000260void CallGraphNode::dump() const { print(dbgs()); }
Chris Lattner03839952005-12-22 06:07:52 +0000261
Chris Lattner75caee22008-04-13 19:41:25 +0000262/// removeCallEdgeFor - This method removes the edge in the node for the
263/// specified call site. Note that this method takes linear time, so it
264/// should be used sparingly.
265void CallGraphNode::removeCallEdgeFor(CallSite CS) {
Chris Lattnerbe577652009-08-31 07:23:46 +0000266 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
267 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
Chris Lattnera541b0f2009-09-01 06:31:31 +0000268 if (I->first == CS.getInstruction()) {
Chris Lattnerbe577652009-08-31 07:23:46 +0000269 I->second->DropRef();
270 *I = CalledFunctions.back();
271 CalledFunctions.pop_back();
272 return;
273 }
Chris Lattner75caee22008-04-13 19:41:25 +0000274 }
275}
276
277
Chris Lattnercd382a32004-09-18 21:34:34 +0000278// removeAnyCallEdgeTo - This method removes any call edges from this node to
279// the specified callee function. This takes more time to execute than
280// removeCallEdgeTo, so it should not be used unless necessary.
281void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
Chris Lattnerfff03c92004-09-19 19:01:06 +0000282 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
Chris Lattnerd85340f2006-07-12 18:29:36 +0000283 if (CalledFunctions[i].second == Callee) {
Chris Lattnerb374b902009-08-31 03:15:49 +0000284 Callee->DropRef();
Chris Lattnerfff03c92004-09-19 19:01:06 +0000285 CalledFunctions[i] = CalledFunctions.back();
286 CalledFunctions.pop_back();
287 --i; --e;
Chris Lattnercd382a32004-09-18 21:34:34 +0000288 }
289}
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000290
Duncan Sandsa2582da2008-10-03 07:36:09 +0000291/// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite
292/// from this node to the specified callee function.
293void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) {
Gabor Greif7f2e3812009-01-17 19:46:01 +0000294 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
295 assert(I != CalledFunctions.end() && "Cannot find callee to remove!");
296 CallRecord &CR = *I;
Chris Lattnera541b0f2009-09-01 06:31:31 +0000297 if (CR.second == Callee && CR.first == 0) {
Chris Lattnerb374b902009-08-31 03:15:49 +0000298 Callee->DropRef();
299 *I = CalledFunctions.back();
300 CalledFunctions.pop_back();
Duncan Sandsa2582da2008-10-03 07:36:09 +0000301 return;
302 }
303 }
304}
305
Chris Lattnera51c39c2009-09-15 05:40:35 +0000306/// replaceCallEdge - This method replaces the edge in the node for the
307/// specified call site with a new one. Note that this method takes linear
308/// time, so it should be used sparingly.
309void CallGraphNode::replaceCallEdge(CallSite CS,
310 CallSite NewCS, CallGraphNode *NewNode){
311 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
312 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
313 if (I->first == CS.getInstruction()) {
314 I->second->DropRef();
315 I->first = NewCS.getInstruction();
316 I->second = NewNode;
317 NewNode->AddRef();
318 return;
319 }
320 }
321}
322
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000323// Enuse that users of CallGraph.h also link with this file
324DEFINING_FILE_FOR(CallGraph)