blob: c59c31c22a936218fd23ddf878b25a7e6d411b90 [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
Dan Gohmanae73dc12008-09-04 17:05:41 +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.
89 virtual void *getAdjustedAnalysisPointer(const PassInfo *PI) {
90 if (PI->isPassID(&CallGraph::ID))
91 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.
129 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I)
Gabor Greifedc4d692009-01-22 21:35:57 +0000130 if ((!isa<CallInst>(I) && !isa<InvokeInst>(I))
131 || !CallSite(cast<Instruction>(I)).isCallee(I)) {
Duncan Sands7ca9d812008-09-09 19:56:34 +0000132 // Not a call, or being used as a parameter rather than as the callee.
133 ExternalCallingNode->addCalledFunction(CallSite(), Node);
134 break;
135 }
136
Chris Lattner03839952005-12-22 06:07:52 +0000137 // If this function is not defined in this translation unit, it could call
138 // anything.
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000139 if (F->isDeclaration() && !F->isIntrinsic())
Chris Lattnerd85340f2006-07-12 18:29:36 +0000140 Node->addCalledFunction(CallSite(), CallsExternalNode);
Chris Lattner03839952005-12-22 06:07:52 +0000141
Duncan Sands99c1a7c2008-09-09 12:40:47 +0000142 // Look for calls by this function.
Chris Lattner03839952005-12-22 06:07:52 +0000143 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
144 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
145 II != IE; ++II) {
Duncan Sands99c1a7c2008-09-09 12:40:47 +0000146 CallSite CS = CallSite::get(II);
Dale Johannesen1f67ce42009-03-19 18:03:56 +0000147 if (CS.getInstruction() && !isa<DbgInfoIntrinsic>(II)) {
Duncan Sands99c1a7c2008-09-09 12:40:47 +0000148 const Function *Callee = CS.getCalledFunction();
149 if (Callee)
150 Node->addCalledFunction(CS, getOrInsertFunction(Callee));
151 else
152 Node->addCalledFunction(CS, CallsExternalNode);
153 }
Chris Lattner03839952005-12-22 06:07:52 +0000154 }
155 }
156
157 //
158 // destroy - Release memory for the call graph
159 virtual void destroy() {
Chris Lattnerf2d9ceb2006-12-05 19:46:12 +0000160 /// CallsExternalNode is not in the function map, delete it explicitly.
Chris Lattner8a39ed72010-04-20 00:47:34 +0000161 CallsExternalNode->allReferencesDropped();
Chris Lattner12d38bf2006-12-04 21:22:45 +0000162 delete CallsExternalNode;
163 CallsExternalNode = 0;
Chris Lattnerf2d9ceb2006-12-05 19:46:12 +0000164 CallGraph::destroy();
Chris Lattner03839952005-12-22 06:07:52 +0000165 }
166};
Chris Lattner41fbf302001-09-28 00:08:15 +0000167
Chris Lattner03839952005-12-22 06:07:52 +0000168} //End anonymous namespace
169
Dan Gohman844731a2008-05-13 00:00:25 +0000170static RegisterAnalysisGroup<CallGraph> X("Call Graph");
171static RegisterPass<BasicCallGraph>
172Y("basiccg", "Basic CallGraph Construction", false, true);
173static RegisterAnalysisGroup<CallGraph, true> Z(Y);
174
Devang Patel19974732007-05-03 01:11:54 +0000175char CallGraph::ID = 0;
176char BasicCallGraph::ID = 0;
Lauro Ramos Venancioc7182882007-05-02 20:37:47 +0000177
Chris Lattner03839952005-12-22 06:07:52 +0000178void CallGraph::initialize(Module &M) {
Chris Lattner7e708292002-06-25 16:13:24 +0000179 Mod = &M;
Chris Lattner41fbf302001-09-28 00:08:15 +0000180}
181
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000182void CallGraph::destroy() {
Chris Lattnerb374b902009-08-31 03:15:49 +0000183 if (FunctionMap.empty()) return;
184
Chris Lattner8a39ed72010-04-20 00:47:34 +0000185 // Reset all node's use counts to zero before deleting them to prevent an
186 // assertion from firing.
187#ifndef NDEBUG
188 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
189 I != E; ++I)
190 I->second->allReferencesDropped();
191#endif
192
Chris Lattnerb374b902009-08-31 03:15:49 +0000193 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
194 I != E; ++I)
195 delete I->second;
196 FunctionMap.clear();
Chris Lattner25e9cad2001-11-26 18:51:25 +0000197}
198
Chris Lattner45cfe542009-08-23 06:03:38 +0000199void CallGraph::print(raw_ostream &OS, Module*) const {
Chris Lattner16500152002-11-04 00:21:19 +0000200 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattneraf8a4242004-08-08 03:27:49 +0000201 I->second->print(OS);
202}
Chris Lattner23603a62009-08-30 22:24:32 +0000203void CallGraph::dump() const {
David Greene265c0262009-12-23 20:03:58 +0000204 print(dbgs(), 0);
Chris Lattner23603a62009-08-30 22:24:32 +0000205}
Chris Lattneraf8a4242004-08-08 03:27:49 +0000206
Chris Lattner25e9cad2001-11-26 18:51:25 +0000207//===----------------------------------------------------------------------===//
208// Implementations of public modification methods
209//
210
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000211// removeFunctionFromModule - Unlink the function from this module, returning
212// it. Because this removes the function from the module, the call graph node
213// is destroyed. This is only valid if the function does not call any other
214// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner25e9cad2001-11-26 18:51:25 +0000215// is to dropAllReferences before calling this.
216//
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000217Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
Chris Lattner2adb8302009-08-31 02:24:20 +0000218 assert(CGN->empty() && "Cannot remove function from call "
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000219 "graph if it references other functions!");
Chris Lattner5714c972003-08-31 20:36:52 +0000220 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000221 delete CGN; // Delete the call graph node for this func
Chris Lattner5714c972003-08-31 20:36:52 +0000222 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner25e9cad2001-11-26 18:51:25 +0000223
Chris Lattner5714c972003-08-31 20:36:52 +0000224 Mod->getFunctionList().remove(F);
225 return F;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000226}
227
Chris Lattnerc54b1c12006-01-14 20:03:00 +0000228// getOrInsertFunction - This method is identical to calling operator[], but
229// it will insert a new CallGraphNode for the specified function if one does
230// not already exist.
231CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
232 CallGraphNode *&CGN = FunctionMap[F];
233 if (CGN) return CGN;
234
235 assert((!F || F->getParent() == Mod) && "Function not in current module!");
236 return CGN = new CallGraphNode(const_cast<Function*>(F));
237}
238
Chris Lattner45cfe542009-08-23 06:03:38 +0000239void CallGraphNode::print(raw_ostream &OS) const {
Chris Lattner03839952005-12-22 06:07:52 +0000240 if (Function *F = getFunction())
Chris Lattnerb374b902009-08-31 03:15:49 +0000241 OS << "Call graph node for function: '" << F->getName() << "'";
Chris Lattner03839952005-12-22 06:07:52 +0000242 else
Chris Lattnerb374b902009-08-31 03:15:49 +0000243 OS << "Call graph node <<null function>>";
244
245 OS << "<<0x" << this << ">> #uses=" << getNumReferences() << '\n';
Chris Lattner03839952005-12-22 06:07:52 +0000246
Duncan Sands28f02122008-09-08 16:04:03 +0000247 for (const_iterator I = begin(), E = end(); I != E; ++I)
Gabor Greif61046262009-01-14 17:09:04 +0000248 if (Function *FI = I->second->getFunction())
Chris Lattner45cfe542009-08-23 06:03:38 +0000249 OS << " Calls function '" << FI->getName() <<"'\n";
Duncan Sands28f02122008-09-08 16:04:03 +0000250 else
251 OS << " Calls external node\n";
Chris Lattner03839952005-12-22 06:07:52 +0000252 OS << "\n";
253}
254
David Greene265c0262009-12-23 20:03:58 +0000255void CallGraphNode::dump() const { print(dbgs()); }
Chris Lattner03839952005-12-22 06:07:52 +0000256
Chris Lattner75caee22008-04-13 19:41:25 +0000257/// removeCallEdgeFor - This method removes the edge in the node for the
258/// specified call site. Note that this method takes linear time, so it
259/// should be used sparingly.
260void CallGraphNode::removeCallEdgeFor(CallSite CS) {
Chris Lattnerbe577652009-08-31 07:23:46 +0000261 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
262 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
Chris Lattnera541b0f2009-09-01 06:31:31 +0000263 if (I->first == CS.getInstruction()) {
Chris Lattnerbe577652009-08-31 07:23:46 +0000264 I->second->DropRef();
265 *I = CalledFunctions.back();
266 CalledFunctions.pop_back();
267 return;
268 }
Chris Lattner75caee22008-04-13 19:41:25 +0000269 }
270}
271
272
Chris Lattnercd382a32004-09-18 21:34:34 +0000273// removeAnyCallEdgeTo - This method removes any call edges from this node to
274// the specified callee function. This takes more time to execute than
275// removeCallEdgeTo, so it should not be used unless necessary.
276void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
Chris Lattnerfff03c92004-09-19 19:01:06 +0000277 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
Chris Lattnerd85340f2006-07-12 18:29:36 +0000278 if (CalledFunctions[i].second == Callee) {
Chris Lattnerb374b902009-08-31 03:15:49 +0000279 Callee->DropRef();
Chris Lattnerfff03c92004-09-19 19:01:06 +0000280 CalledFunctions[i] = CalledFunctions.back();
281 CalledFunctions.pop_back();
282 --i; --e;
Chris Lattnercd382a32004-09-18 21:34:34 +0000283 }
284}
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000285
Duncan Sandsa2582da2008-10-03 07:36:09 +0000286/// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite
287/// from this node to the specified callee function.
288void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) {
Gabor Greif7f2e3812009-01-17 19:46:01 +0000289 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
290 assert(I != CalledFunctions.end() && "Cannot find callee to remove!");
291 CallRecord &CR = *I;
Chris Lattnera541b0f2009-09-01 06:31:31 +0000292 if (CR.second == Callee && CR.first == 0) {
Chris Lattnerb374b902009-08-31 03:15:49 +0000293 Callee->DropRef();
294 *I = CalledFunctions.back();
295 CalledFunctions.pop_back();
Duncan Sandsa2582da2008-10-03 07:36:09 +0000296 return;
297 }
298 }
299}
300
Chris Lattnera51c39c2009-09-15 05:40:35 +0000301/// replaceCallEdge - This method replaces the edge in the node for the
302/// specified call site with a new one. Note that this method takes linear
303/// time, so it should be used sparingly.
304void CallGraphNode::replaceCallEdge(CallSite CS,
305 CallSite NewCS, CallGraphNode *NewNode){
306 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
307 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
308 if (I->first == CS.getInstruction()) {
309 I->second->DropRef();
310 I->first = NewCS.getInstruction();
311 I->second = NewNode;
312 NewNode->AddRef();
313 return;
314 }
315 }
316}
317
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000318// Enuse that users of CallGraph.h also link with this file
319DEFINING_FILE_FOR(CallGraph)