blob: 9cd8bb8c2df1b9d38be8ddce72ad6d3d83a3eb4e [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"
Chris Lattner45cfe542009-08-23 06:03:38 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattnerb81c0212004-04-12 05:36:32 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Chris Lattner03839952005-12-22 06:07:52 +000023namespace {
24
25//===----------------------------------------------------------------------===//
26// BasicCallGraph class definition
Chris Lattner41fbf302001-09-28 00:08:15 +000027//
Nick Lewycky6726b6d2009-10-25 06:33:48 +000028class BasicCallGraph : public CallGraph, public ModulePass {
Chris Lattner03839952005-12-22 06:07:52 +000029 // Root is root of the call graph, or the external node if a 'main' function
30 // couldn't be found.
31 //
32 CallGraphNode *Root;
Chris Lattner41fbf302001-09-28 00:08:15 +000033
Chris Lattner03839952005-12-22 06:07:52 +000034 // ExternalCallingNode - This node has edges to all external functions and
35 // those internal functions that have their address taken.
36 CallGraphNode *ExternalCallingNode;
Chris Lattner25e9cad2001-11-26 18:51:25 +000037
Chris Lattner03839952005-12-22 06:07:52 +000038 // CallsExternalNode - This node has edges to it from all functions making
39 // indirect calls or calling an external function.
40 CallGraphNode *CallsExternalNode;
41
42public:
Devang Patel19974732007-05-03 01:11:54 +000043 static char ID; // Class identification, replacement for typeinfo
Dan Gohmanae73dc12008-09-04 17:05:41 +000044 BasicCallGraph() : ModulePass(&ID), Root(0),
Devang Patel794fd752007-05-01 21:15:47 +000045 ExternalCallingNode(0), CallsExternalNode(0) {}
Chris Lattner03839952005-12-22 06:07:52 +000046
47 // runOnModule - Compute the call graph for the specified module.
48 virtual bool runOnModule(Module &M) {
Chris Lattner03839952005-12-22 06:07:52 +000049 CallGraph::initialize(M);
50
Chris Lattnerc54b1c12006-01-14 20:03:00 +000051 ExternalCallingNode = getOrInsertFunction(0);
Chris Lattner03839952005-12-22 06:07:52 +000052 CallsExternalNode = new CallGraphNode(0);
53 Root = 0;
54
Chris Lattnerb374b902009-08-31 03:15:49 +000055 // Add every function to the call graph.
Chris Lattner03839952005-12-22 06:07:52 +000056 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
57 addToCallGraph(I);
58
59 // If we didn't find a main function, use the external call graph node
60 if (Root == 0) Root = ExternalCallingNode;
61
62 return false;
Chris Lattnerd4d427b2002-03-06 20:19:35 +000063 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000064
Chris Lattner03839952005-12-22 06:07:52 +000065 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
66 AU.setPreservesAll();
67 }
Chris Lattnerd4d427b2002-03-06 20:19:35 +000068
Chris Lattner45cfe542009-08-23 06:03:38 +000069 virtual void print(raw_ostream &OS, const Module *) const {
70 OS << "CallGraph Root is: ";
Chris Lattner03839952005-12-22 06:07:52 +000071 if (Function *F = getRoot()->getFunction())
Chris Lattner45cfe542009-08-23 06:03:38 +000072 OS << F->getName() << "\n";
73 else {
74 OS << "<<null function: 0x" << getRoot() << ">>\n";
75 }
Chris Lattner03839952005-12-22 06:07:52 +000076
Chris Lattner45cfe542009-08-23 06:03:38 +000077 CallGraph::print(OS, 0);
Chris Lattner03839952005-12-22 06:07:52 +000078 }
79
80 virtual void releaseMemory() {
81 destroy();
82 }
83
Chris Lattner03839952005-12-22 06:07:52 +000084 CallGraphNode* getExternalCallingNode() const { return ExternalCallingNode; }
85 CallGraphNode* getCallsExternalNode() const { return CallsExternalNode; }
86
87 // getRoot - Return the root of the call graph, which is either main, or if
88 // main cannot be found, the external node.
Chris Lattnerd4d427b2002-03-06 20:19:35 +000089 //
Chris Lattner03839952005-12-22 06:07:52 +000090 CallGraphNode *getRoot() { return Root; }
91 const CallGraphNode *getRoot() const { return Root; }
92
93private:
94 //===---------------------------------------------------------------------
95 // Implementation of CallGraph construction
96 //
Chris Lattner03839952005-12-22 06:07:52 +000097
Chris Lattner03839952005-12-22 06:07:52 +000098 // addToCallGraph - Add a function to the call graph, and link the node to all
99 // of the functions that it calls.
100 //
101 void addToCallGraph(Function *F) {
Chris Lattnerc54b1c12006-01-14 20:03:00 +0000102 CallGraphNode *Node = getOrInsertFunction(F);
Chris Lattner03839952005-12-22 06:07:52 +0000103
Chris Lattnerd85340f2006-07-12 18:29:36 +0000104 // If this function has external linkage, anything could call it.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000105 if (!F->hasLocalLinkage()) {
Chris Lattnerd85340f2006-07-12 18:29:36 +0000106 ExternalCallingNode->addCalledFunction(CallSite(), Node);
Chris Lattner03839952005-12-22 06:07:52 +0000107
108 // Found the entry point?
109 if (F->getName() == "main") {
110 if (Root) // Found multiple external mains? Don't pick one.
111 Root = ExternalCallingNode;
112 else
113 Root = Node; // Found a main, keep track of it!
114 }
115 }
116
Duncan Sands7ca9d812008-09-09 19:56:34 +0000117 // Loop over all of the users of the function, looking for non-call uses.
118 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I)
Gabor Greifedc4d692009-01-22 21:35:57 +0000119 if ((!isa<CallInst>(I) && !isa<InvokeInst>(I))
120 || !CallSite(cast<Instruction>(I)).isCallee(I)) {
Duncan Sands7ca9d812008-09-09 19:56:34 +0000121 // Not a call, or being used as a parameter rather than as the callee.
122 ExternalCallingNode->addCalledFunction(CallSite(), Node);
123 break;
124 }
125
Chris Lattner03839952005-12-22 06:07:52 +0000126 // If this function is not defined in this translation unit, it could call
127 // anything.
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000128 if (F->isDeclaration() && !F->isIntrinsic())
Chris Lattnerd85340f2006-07-12 18:29:36 +0000129 Node->addCalledFunction(CallSite(), CallsExternalNode);
Chris Lattner03839952005-12-22 06:07:52 +0000130
Duncan Sands99c1a7c2008-09-09 12:40:47 +0000131 // Look for calls by this function.
Chris Lattner03839952005-12-22 06:07:52 +0000132 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
133 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
134 II != IE; ++II) {
Duncan Sands99c1a7c2008-09-09 12:40:47 +0000135 CallSite CS = CallSite::get(II);
Dale Johannesen1f67ce42009-03-19 18:03:56 +0000136 if (CS.getInstruction() && !isa<DbgInfoIntrinsic>(II)) {
Duncan Sands99c1a7c2008-09-09 12:40:47 +0000137 const Function *Callee = CS.getCalledFunction();
138 if (Callee)
139 Node->addCalledFunction(CS, getOrInsertFunction(Callee));
140 else
141 Node->addCalledFunction(CS, CallsExternalNode);
142 }
Chris Lattner03839952005-12-22 06:07:52 +0000143 }
144 }
145
146 //
147 // destroy - Release memory for the call graph
148 virtual void destroy() {
Chris Lattnerf2d9ceb2006-12-05 19:46:12 +0000149 /// CallsExternalNode is not in the function map, delete it explicitly.
Chris Lattner12d38bf2006-12-04 21:22:45 +0000150 delete CallsExternalNode;
151 CallsExternalNode = 0;
Chris Lattnerf2d9ceb2006-12-05 19:46:12 +0000152 CallGraph::destroy();
Chris Lattner03839952005-12-22 06:07:52 +0000153 }
154};
Chris Lattner41fbf302001-09-28 00:08:15 +0000155
Chris Lattner03839952005-12-22 06:07:52 +0000156} //End anonymous namespace
157
Dan Gohman844731a2008-05-13 00:00:25 +0000158static RegisterAnalysisGroup<CallGraph> X("Call Graph");
159static RegisterPass<BasicCallGraph>
160Y("basiccg", "Basic CallGraph Construction", false, true);
161static RegisterAnalysisGroup<CallGraph, true> Z(Y);
162
Devang Patel19974732007-05-03 01:11:54 +0000163char CallGraph::ID = 0;
164char BasicCallGraph::ID = 0;
Lauro Ramos Venancioc7182882007-05-02 20:37:47 +0000165
Chris Lattner03839952005-12-22 06:07:52 +0000166void CallGraph::initialize(Module &M) {
Chris Lattner7e708292002-06-25 16:13:24 +0000167 Mod = &M;
Chris Lattner41fbf302001-09-28 00:08:15 +0000168}
169
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000170void CallGraph::destroy() {
Chris Lattnerb374b902009-08-31 03:15:49 +0000171 if (FunctionMap.empty()) return;
172
173 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
174 I != E; ++I)
175 delete I->second;
176 FunctionMap.clear();
Chris Lattner25e9cad2001-11-26 18:51:25 +0000177}
178
Chris Lattner45cfe542009-08-23 06:03:38 +0000179void CallGraph::print(raw_ostream &OS, Module*) const {
Chris Lattner16500152002-11-04 00:21:19 +0000180 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattneraf8a4242004-08-08 03:27:49 +0000181 I->second->print(OS);
182}
Chris Lattner23603a62009-08-30 22:24:32 +0000183void CallGraph::dump() const {
184 print(errs(), 0);
185}
Chris Lattneraf8a4242004-08-08 03:27:49 +0000186
Chris Lattner25e9cad2001-11-26 18:51:25 +0000187//===----------------------------------------------------------------------===//
188// Implementations of public modification methods
189//
190
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000191// removeFunctionFromModule - Unlink the function from this module, returning
192// it. Because this removes the function from the module, the call graph node
193// is destroyed. This is only valid if the function does not call any other
194// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner25e9cad2001-11-26 18:51:25 +0000195// is to dropAllReferences before calling this.
196//
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000197Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
Chris Lattner2adb8302009-08-31 02:24:20 +0000198 assert(CGN->empty() && "Cannot remove function from call "
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000199 "graph if it references other functions!");
Chris Lattner5714c972003-08-31 20:36:52 +0000200 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000201 delete CGN; // Delete the call graph node for this func
Chris Lattner5714c972003-08-31 20:36:52 +0000202 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner25e9cad2001-11-26 18:51:25 +0000203
Chris Lattner5714c972003-08-31 20:36:52 +0000204 Mod->getFunctionList().remove(F);
205 return F;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000206}
207
Chris Lattnerc54b1c12006-01-14 20:03:00 +0000208// getOrInsertFunction - This method is identical to calling operator[], but
209// it will insert a new CallGraphNode for the specified function if one does
210// not already exist.
211CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
212 CallGraphNode *&CGN = FunctionMap[F];
213 if (CGN) return CGN;
214
215 assert((!F || F->getParent() == Mod) && "Function not in current module!");
216 return CGN = new CallGraphNode(const_cast<Function*>(F));
217}
218
Chris Lattner45cfe542009-08-23 06:03:38 +0000219void CallGraphNode::print(raw_ostream &OS) const {
Chris Lattner03839952005-12-22 06:07:52 +0000220 if (Function *F = getFunction())
Chris Lattnerb374b902009-08-31 03:15:49 +0000221 OS << "Call graph node for function: '" << F->getName() << "'";
Chris Lattner03839952005-12-22 06:07:52 +0000222 else
Chris Lattnerb374b902009-08-31 03:15:49 +0000223 OS << "Call graph node <<null function>>";
224
225 OS << "<<0x" << this << ">> #uses=" << getNumReferences() << '\n';
Chris Lattner03839952005-12-22 06:07:52 +0000226
Duncan Sands28f02122008-09-08 16:04:03 +0000227 for (const_iterator I = begin(), E = end(); I != E; ++I)
Gabor Greif61046262009-01-14 17:09:04 +0000228 if (Function *FI = I->second->getFunction())
Chris Lattner45cfe542009-08-23 06:03:38 +0000229 OS << " Calls function '" << FI->getName() <<"'\n";
Duncan Sands28f02122008-09-08 16:04:03 +0000230 else
231 OS << " Calls external node\n";
Chris Lattner03839952005-12-22 06:07:52 +0000232 OS << "\n";
233}
234
Chris Lattner45cfe542009-08-23 06:03:38 +0000235void CallGraphNode::dump() const { print(errs()); }
Chris Lattner03839952005-12-22 06:07:52 +0000236
Chris Lattner75caee22008-04-13 19:41:25 +0000237/// removeCallEdgeFor - This method removes the edge in the node for the
238/// specified call site. Note that this method takes linear time, so it
239/// should be used sparingly.
240void CallGraphNode::removeCallEdgeFor(CallSite CS) {
Chris Lattnerbe577652009-08-31 07:23:46 +0000241 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
242 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
Chris Lattnera541b0f2009-09-01 06:31:31 +0000243 if (I->first == CS.getInstruction()) {
Chris Lattnerbe577652009-08-31 07:23:46 +0000244 I->second->DropRef();
245 *I = CalledFunctions.back();
246 CalledFunctions.pop_back();
247 return;
248 }
Chris Lattner75caee22008-04-13 19:41:25 +0000249 }
250}
251
252
Chris Lattnercd382a32004-09-18 21:34:34 +0000253// removeAnyCallEdgeTo - This method removes any call edges from this node to
254// the specified callee function. This takes more time to execute than
255// removeCallEdgeTo, so it should not be used unless necessary.
256void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
Chris Lattnerfff03c92004-09-19 19:01:06 +0000257 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
Chris Lattnerd85340f2006-07-12 18:29:36 +0000258 if (CalledFunctions[i].second == Callee) {
Chris Lattnerb374b902009-08-31 03:15:49 +0000259 Callee->DropRef();
Chris Lattnerfff03c92004-09-19 19:01:06 +0000260 CalledFunctions[i] = CalledFunctions.back();
261 CalledFunctions.pop_back();
262 --i; --e;
Chris Lattnercd382a32004-09-18 21:34:34 +0000263 }
264}
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000265
Duncan Sandsa2582da2008-10-03 07:36:09 +0000266/// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite
267/// from this node to the specified callee function.
268void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) {
Gabor Greif7f2e3812009-01-17 19:46:01 +0000269 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
270 assert(I != CalledFunctions.end() && "Cannot find callee to remove!");
271 CallRecord &CR = *I;
Chris Lattnera541b0f2009-09-01 06:31:31 +0000272 if (CR.second == Callee && CR.first == 0) {
Chris Lattnerb374b902009-08-31 03:15:49 +0000273 Callee->DropRef();
274 *I = CalledFunctions.back();
275 CalledFunctions.pop_back();
Duncan Sandsa2582da2008-10-03 07:36:09 +0000276 return;
277 }
278 }
279}
280
Chris Lattnera51c39c2009-09-15 05:40:35 +0000281/// replaceCallEdge - This method replaces the edge in the node for the
282/// specified call site with a new one. Note that this method takes linear
283/// time, so it should be used sparingly.
284void CallGraphNode::replaceCallEdge(CallSite CS,
285 CallSite NewCS, CallGraphNode *NewNode){
286 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
287 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
288 if (I->first == CS.getInstruction()) {
289 I->second->DropRef();
290 I->first = NewCS.getInstruction();
291 I->second = NewNode;
292 NewNode->AddRef();
293 return;
294 }
295 }
296}
297
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000298// Enuse that users of CallGraph.h also link with this file
299DEFINING_FILE_FOR(CallGraph)