blob: fba1d00d48c067b29fb6371b42115ed0ee65dfab [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"
Chris Lattner07a38e72003-10-31 21:05:12 +000018#include "llvm/Support/CallSite.h"
Reid Spencerd7d83db2007-02-05 23:42:17 +000019#include "llvm/Support/Compiler.h"
Bill Wendling68fe61d2006-11-29 00:19:40 +000020#include "llvm/Support/Streams.h"
21#include <ostream>
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//
Reid Spencerd7d83db2007-02-05 23:42:17 +000029class VISIBILITY_HIDDEN BasicCallGraph : public CallGraph, public ModulePass {
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
56 // Add every function to the call graph...
57 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
Bill Wendling5c7e3262006-12-17 05:15:13 +000070 void print(std::ostream *o, const Module *M) const {
71 if (o) print(*o, M);
Bill Wendling68fe61d2006-11-29 00:19:40 +000072 }
73
Chris Lattner03839952005-12-22 06:07:52 +000074 virtual void print(std::ostream &o, const Module *M) const {
75 o << "CallGraph Root is: ";
76 if (Function *F = getRoot()->getFunction())
77 o << F->getName() << "\n";
78 else
79 o << "<<null function: 0x" << getRoot() << ">>\n";
80
81 CallGraph::print(o, M);
82 }
83
84 virtual void releaseMemory() {
85 destroy();
86 }
87
88 /// dump - Print out this call graph.
89 ///
90 inline void dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +000091 print(cerr, Mod);
Chris Lattner03839952005-12-22 06:07:52 +000092 }
93
94 CallGraphNode* getExternalCallingNode() const { return ExternalCallingNode; }
95 CallGraphNode* getCallsExternalNode() const { return CallsExternalNode; }
96
97 // getRoot - Return the root of the call graph, which is either main, or if
98 // main cannot be found, the external node.
Chris Lattnerd4d427b2002-03-06 20:19:35 +000099 //
Chris Lattner03839952005-12-22 06:07:52 +0000100 CallGraphNode *getRoot() { return Root; }
101 const CallGraphNode *getRoot() const { return Root; }
102
103private:
104 //===---------------------------------------------------------------------
105 // Implementation of CallGraph construction
106 //
Chris Lattner03839952005-12-22 06:07:52 +0000107
Chris Lattner03839952005-12-22 06:07:52 +0000108 // addToCallGraph - Add a function to the call graph, and link the node to all
109 // of the functions that it calls.
110 //
111 void addToCallGraph(Function *F) {
Chris Lattnerc54b1c12006-01-14 20:03:00 +0000112 CallGraphNode *Node = getOrInsertFunction(F);
Chris Lattner03839952005-12-22 06:07:52 +0000113
Chris Lattnerd85340f2006-07-12 18:29:36 +0000114 // If this function has external linkage, anything could call it.
Chris Lattner03839952005-12-22 06:07:52 +0000115 if (!F->hasInternalLinkage()) {
Chris Lattnerd85340f2006-07-12 18:29:36 +0000116 ExternalCallingNode->addCalledFunction(CallSite(), Node);
Chris Lattner03839952005-12-22 06:07:52 +0000117
118 // Found the entry point?
119 if (F->getName() == "main") {
120 if (Root) // Found multiple external mains? Don't pick one.
121 Root = ExternalCallingNode;
122 else
123 Root = Node; // Found a main, keep track of it!
124 }
125 }
126
Duncan Sands7ca9d812008-09-09 19:56:34 +0000127 // Loop over all of the users of the function, looking for non-call uses.
128 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I)
129 if ((!isa<CallInst>(*I) && !isa<InvokeInst>(*I)) || I.getOperandNo()) {
130 // Not a call, or being used as a parameter rather than as the callee.
131 ExternalCallingNode->addCalledFunction(CallSite(), Node);
132 break;
133 }
134
Chris Lattner03839952005-12-22 06:07:52 +0000135 // If this function is not defined in this translation unit, it could call
136 // anything.
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000137 if (F->isDeclaration() && !F->isIntrinsic())
Chris Lattnerd85340f2006-07-12 18:29:36 +0000138 Node->addCalledFunction(CallSite(), CallsExternalNode);
Chris Lattner03839952005-12-22 06:07:52 +0000139
Duncan Sands99c1a7c2008-09-09 12:40:47 +0000140 // Look for calls by this function.
Chris Lattner03839952005-12-22 06:07:52 +0000141 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
142 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
143 II != IE; ++II) {
Duncan Sands99c1a7c2008-09-09 12:40:47 +0000144 CallSite CS = CallSite::get(II);
145 if (CS.getInstruction()) {
146 const Function *Callee = CS.getCalledFunction();
147 if (Callee)
148 Node->addCalledFunction(CS, getOrInsertFunction(Callee));
149 else
150 Node->addCalledFunction(CS, CallsExternalNode);
151 }
Chris Lattner03839952005-12-22 06:07:52 +0000152 }
153 }
154
155 //
156 // destroy - Release memory for the call graph
157 virtual void destroy() {
Chris Lattnerf2d9ceb2006-12-05 19:46:12 +0000158 /// CallsExternalNode is not in the function map, delete it explicitly.
Chris Lattner12d38bf2006-12-04 21:22:45 +0000159 delete CallsExternalNode;
160 CallsExternalNode = 0;
Chris Lattnerf2d9ceb2006-12-05 19:46:12 +0000161 CallGraph::destroy();
Chris Lattner03839952005-12-22 06:07:52 +0000162 }
163};
Chris Lattner41fbf302001-09-28 00:08:15 +0000164
Chris Lattner03839952005-12-22 06:07:52 +0000165} //End anonymous namespace
166
Dan Gohman844731a2008-05-13 00:00:25 +0000167static RegisterAnalysisGroup<CallGraph> X("Call Graph");
168static RegisterPass<BasicCallGraph>
169Y("basiccg", "Basic CallGraph Construction", false, true);
170static RegisterAnalysisGroup<CallGraph, true> Z(Y);
171
Devang Patel19974732007-05-03 01:11:54 +0000172char CallGraph::ID = 0;
173char BasicCallGraph::ID = 0;
Lauro Ramos Venancioc7182882007-05-02 20:37:47 +0000174
Chris Lattner03839952005-12-22 06:07:52 +0000175void CallGraph::initialize(Module &M) {
Chris Lattner7e708292002-06-25 16:13:24 +0000176 Mod = &M;
Chris Lattner41fbf302001-09-28 00:08:15 +0000177}
178
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000179void CallGraph::destroy() {
Chris Lattner45d10472006-10-09 17:28:13 +0000180 if (!FunctionMap.empty()) {
Chris Lattner03839952005-12-22 06:07:52 +0000181 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
182 I != E; ++I)
183 delete I->second;
184 FunctionMap.clear();
185 }
Chris Lattner25e9cad2001-11-26 18:51:25 +0000186}
187
Chris Lattneraf8a4242004-08-08 03:27:49 +0000188void CallGraph::print(std::ostream &OS, const Module *M) const {
Chris Lattner16500152002-11-04 00:21:19 +0000189 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattneraf8a4242004-08-08 03:27:49 +0000190 I->second->print(OS);
191}
192
Chris Lattner1694ec62006-01-14 19:17:02 +0000193void CallGraph::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000194 print(cerr, 0);
Chris Lattner1694ec62006-01-14 19:17:02 +0000195}
196
Chris Lattner25e9cad2001-11-26 18:51:25 +0000197//===----------------------------------------------------------------------===//
198// Implementations of public modification methods
199//
200
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000201// removeFunctionFromModule - Unlink the function from this module, returning
202// it. Because this removes the function from the module, the call graph node
203// is destroyed. This is only valid if the function does not call any other
204// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner25e9cad2001-11-26 18:51:25 +0000205// is to dropAllReferences before calling this.
206//
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000207Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
208 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
209 "graph if it references other functions!");
Chris Lattner5714c972003-08-31 20:36:52 +0000210 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000211 delete CGN; // Delete the call graph node for this func
Chris Lattner5714c972003-08-31 20:36:52 +0000212 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner25e9cad2001-11-26 18:51:25 +0000213
Chris Lattner5714c972003-08-31 20:36:52 +0000214 Mod->getFunctionList().remove(F);
215 return F;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000216}
217
Chris Lattner6f7e5eb2004-09-18 00:22:13 +0000218// changeFunction - This method changes the function associated with this
219// CallGraphNode, for use by transformations that need to change the prototype
220// of a Function (thus they must create a new Function and move the old code
221// over).
222void CallGraph::changeFunction(Function *OldF, Function *NewF) {
223 iterator I = FunctionMap.find(OldF);
224 CallGraphNode *&New = FunctionMap[NewF];
225 assert(I != FunctionMap.end() && I->second && !New &&
226 "OldF didn't exist in CG or NewF already does!");
227 New = I->second;
Chris Lattner3795bc92004-09-18 00:27:20 +0000228 New->F = NewF;
Chris Lattner6f7e5eb2004-09-18 00:22:13 +0000229 FunctionMap.erase(I);
230}
231
Chris Lattnerc54b1c12006-01-14 20:03:00 +0000232// getOrInsertFunction - This method is identical to calling operator[], but
233// it will insert a new CallGraphNode for the specified function if one does
234// not already exist.
235CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
236 CallGraphNode *&CGN = FunctionMap[F];
237 if (CGN) return CGN;
238
239 assert((!F || F->getParent() == Mod) && "Function not in current module!");
240 return CGN = new CallGraphNode(const_cast<Function*>(F));
241}
242
Chris Lattner03839952005-12-22 06:07:52 +0000243void CallGraphNode::print(std::ostream &OS) const {
244 if (Function *F = getFunction())
245 OS << "Call graph node for function: '" << F->getName() <<"'\n";
246 else
247 OS << "Call graph node <<null function: 0x" << this << ">>:\n";
248
Duncan Sands28f02122008-09-08 16:04:03 +0000249 for (const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerd85340f2006-07-12 18:29:36 +0000250 if (I->second->getFunction())
Duncan Sands28f02122008-09-08 16:04:03 +0000251 OS << " Calls function '" << I->second->getFunction()->getName() <<"'\n";
252 else
253 OS << " Calls external node\n";
Chris Lattner03839952005-12-22 06:07:52 +0000254 OS << "\n";
255}
256
Bill Wendlinge8156192006-12-07 01:30:32 +0000257void CallGraphNode::dump() const { print(cerr); }
Chris Lattner03839952005-12-22 06:07:52 +0000258
Chris Lattner75caee22008-04-13 19:41:25 +0000259/// removeCallEdgeFor - This method removes the edge in the node for the
260/// specified call site. Note that this method takes linear time, so it
261/// should be used sparingly.
262void CallGraphNode::removeCallEdgeFor(CallSite CS) {
263 for (unsigned i = CalledFunctions.size(); ; --i) {
Duncan Sandsfec2c2b2008-09-06 17:19:29 +0000264 assert(i && "Cannot find callsite to remove!");
Chris Lattner75caee22008-04-13 19:41:25 +0000265 if (CalledFunctions[i-1].first == CS) {
266 CalledFunctions.erase(CalledFunctions.begin()+i-1);
267 return;
268 }
269 }
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 Lattnerfff03c92004-09-19 19:01:06 +0000279 CalledFunctions[i] = CalledFunctions.back();
280 CalledFunctions.pop_back();
281 --i; --e;
Chris Lattnercd382a32004-09-18 21:34:34 +0000282 }
283}
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000284
Duncan Sandsfec2c2b2008-09-06 17:19:29 +0000285/// replaceCallSite - Make the edge in the node for Old CallSite be for
286/// New CallSite instead. Note that this method takes linear time, so it
287/// should be used sparingly.
288void CallGraphNode::replaceCallSite(CallSite Old, CallSite New) {
289 for (unsigned i = CalledFunctions.size(); ; --i) {
290 assert(i && "Cannot find callsite to replace!");
291 if (CalledFunctions[i-1].first == Old) {
292 CalledFunctions[i-1].first = New;
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)