blob: 5616ee9a4b034ece5fbaa82bd3b6b435fed04f56 [file] [log] [blame]
Chris Lattnerd852cc32002-03-06 18:00:49 +00001//===- CallGraph.cpp - Build a Module's call graph ------------------------===//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman01808ca2005-04-21 21:13:18 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerbbf3ae82001-09-28 00:08:15 +00009//
Chris Lattnerbe1987772005-12-22 06:07:52 +000010// This file implements the CallGraph class and provides the BasicCallGraph
11// default implementation.
Chris Lattner8cbbbef2001-10-13 06:33:19 +000012//
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000013//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/CallGraph.h"
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000016#include "llvm/Module.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000017#include "llvm/Instructions.h"
Chris Lattner9c4a58b2003-10-31 21:05:12 +000018#include "llvm/Support/CallSite.h"
Reid Spencerf75727a2007-02-05 23:42:17 +000019#include "llvm/Support/Compiler.h"
Bill Wendlingafd54eb2006-11-29 00:19:40 +000020#include "llvm/Support/Streams.h"
21#include <ostream>
Chris Lattner8b6db182004-04-12 05:36:32 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Chris Lattnerbaf6c542006-12-04 21:22:45 +000024/// isOnlyADirectCall - Return true if this callsite is *just* a direct call to
25/// the specified function. Specifically return false if the callsite also
26/// takes the address of the function.
Chris Lattner9c4a58b2003-10-31 21:05:12 +000027static bool isOnlyADirectCall(Function *F, CallSite CS) {
28 if (!CS.getInstruction()) return false;
Matthijs Kooijman2353f352008-06-04 16:57:50 +000029 return !CS.hasArgument(F);
Chris Lattner9c4a58b2003-10-31 21:05:12 +000030}
31
Chris Lattnerbe1987772005-12-22 06:07:52 +000032namespace {
33
34//===----------------------------------------------------------------------===//
35// BasicCallGraph class definition
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000036//
Reid Spencerf75727a2007-02-05 23:42:17 +000037class VISIBILITY_HIDDEN BasicCallGraph : public CallGraph, public ModulePass {
Chris Lattnerbe1987772005-12-22 06:07:52 +000038 // Root is root of the call graph, or the external node if a 'main' function
39 // couldn't be found.
40 //
41 CallGraphNode *Root;
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000042
Chris Lattnerbe1987772005-12-22 06:07:52 +000043 // ExternalCallingNode - This node has edges to all external functions and
44 // those internal functions that have their address taken.
45 CallGraphNode *ExternalCallingNode;
Chris Lattner03946cd2001-11-26 18:51:25 +000046
Chris Lattnerbe1987772005-12-22 06:07:52 +000047 // CallsExternalNode - This node has edges to it from all functions making
48 // indirect calls or calling an external function.
49 CallGraphNode *CallsExternalNode;
50
51public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000052 static char ID; // Class identification, replacement for typeinfo
Dan Gohmana79db302008-09-04 17:05:41 +000053 BasicCallGraph() : ModulePass(&ID), Root(0),
Devang Patel09f162c2007-05-01 21:15:47 +000054 ExternalCallingNode(0), CallsExternalNode(0) {}
Chris Lattnerbe1987772005-12-22 06:07:52 +000055
56 // runOnModule - Compute the call graph for the specified module.
57 virtual bool runOnModule(Module &M) {
Chris Lattnerbe1987772005-12-22 06:07:52 +000058 CallGraph::initialize(M);
59
Chris Lattner00ca8d22006-01-14 20:03:00 +000060 ExternalCallingNode = getOrInsertFunction(0);
Chris Lattnerbe1987772005-12-22 06:07:52 +000061 CallsExternalNode = new CallGraphNode(0);
62 Root = 0;
63
64 // Add every function to the call graph...
65 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
66 addToCallGraph(I);
67
68 // If we didn't find a main function, use the external call graph node
69 if (Root == 0) Root = ExternalCallingNode;
70
71 return false;
Chris Lattnerbeed7422002-03-06 20:19:35 +000072 }
Misha Brukman01808ca2005-04-21 21:13:18 +000073
Chris Lattnerbe1987772005-12-22 06:07:52 +000074 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
75 AU.setPreservesAll();
76 }
Chris Lattnerbeed7422002-03-06 20:19:35 +000077
Bill Wendlinga77f1422006-12-17 05:15:13 +000078 void print(std::ostream *o, const Module *M) const {
79 if (o) print(*o, M);
Bill Wendlingafd54eb2006-11-29 00:19:40 +000080 }
81
Chris Lattnerbe1987772005-12-22 06:07:52 +000082 virtual void print(std::ostream &o, const Module *M) const {
83 o << "CallGraph Root is: ";
84 if (Function *F = getRoot()->getFunction())
85 o << F->getName() << "\n";
86 else
87 o << "<<null function: 0x" << getRoot() << ">>\n";
88
89 CallGraph::print(o, M);
90 }
91
92 virtual void releaseMemory() {
93 destroy();
94 }
95
96 /// dump - Print out this call graph.
97 ///
98 inline void dump() const {
Bill Wendlingf3baad32006-12-07 01:30:32 +000099 print(cerr, Mod);
Chris Lattnerbe1987772005-12-22 06:07:52 +0000100 }
101
102 CallGraphNode* getExternalCallingNode() const { return ExternalCallingNode; }
103 CallGraphNode* getCallsExternalNode() const { return CallsExternalNode; }
104
105 // getRoot - Return the root of the call graph, which is either main, or if
106 // main cannot be found, the external node.
Chris Lattnerbeed7422002-03-06 20:19:35 +0000107 //
Chris Lattnerbe1987772005-12-22 06:07:52 +0000108 CallGraphNode *getRoot() { return Root; }
109 const CallGraphNode *getRoot() const { return Root; }
110
111private:
112 //===---------------------------------------------------------------------
113 // Implementation of CallGraph construction
114 //
Chris Lattnerbe1987772005-12-22 06:07:52 +0000115
Chris Lattnerbe1987772005-12-22 06:07:52 +0000116 // addToCallGraph - Add a function to the call graph, and link the node to all
117 // of the functions that it calls.
118 //
119 void addToCallGraph(Function *F) {
Chris Lattner00ca8d22006-01-14 20:03:00 +0000120 CallGraphNode *Node = getOrInsertFunction(F);
Chris Lattnerbe1987772005-12-22 06:07:52 +0000121
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000122 // If this function has external linkage, anything could call it.
Chris Lattnerbe1987772005-12-22 06:07:52 +0000123 if (!F->hasInternalLinkage()) {
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000124 ExternalCallingNode->addCalledFunction(CallSite(), Node);
Chris Lattnerbe1987772005-12-22 06:07:52 +0000125
126 // Found the entry point?
127 if (F->getName() == "main") {
128 if (Root) // Found multiple external mains? Don't pick one.
129 Root = ExternalCallingNode;
130 else
131 Root = Node; // Found a main, keep track of it!
132 }
133 }
134
135 // If this function is not defined in this translation unit, it could call
136 // anything.
Duncan Sands38ef3a82007-12-03 20:06:50 +0000137 if (F->isDeclaration() && !F->isIntrinsic())
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000138 Node->addCalledFunction(CallSite(), CallsExternalNode);
Chris Lattnerbe1987772005-12-22 06:07:52 +0000139
140 // Loop over all of the users of the function... looking for callers...
141 //
142 bool isUsedExternally = false;
143 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I){
144 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000145 CallSite CS = CallSite::get(Inst);
146 if (isOnlyADirectCall(F, CS))
Chris Lattner00ca8d22006-01-14 20:03:00 +0000147 getOrInsertFunction(Inst->getParent()->getParent())
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000148 ->addCalledFunction(CS, Node);
Chris Lattnerbe1987772005-12-22 06:07:52 +0000149 else
150 isUsedExternally = true;
151 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(*I)) {
152 for (Value::use_iterator I = GV->use_begin(), E = GV->use_end();
153 I != E; ++I)
154 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000155 CallSite CS = CallSite::get(Inst);
156 if (isOnlyADirectCall(F, CS))
157 getOrInsertFunction(Inst->getParent()->getParent())
158 ->addCalledFunction(CS, Node);
159 else
160 isUsedExternally = true;
Chris Lattnerbe1987772005-12-22 06:07:52 +0000161 } else {
162 isUsedExternally = true;
163 }
164 } else { // Can't classify the user!
165 isUsedExternally = true;
166 }
Chris Lattner9c4a58b2003-10-31 21:05:12 +0000167 }
Chris Lattnerbe1987772005-12-22 06:07:52 +0000168 if (isUsedExternally)
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000169 ExternalCallingNode->addCalledFunction(CallSite(), Node);
Chris Lattnerbeed7422002-03-06 20:19:35 +0000170
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000171 // Look for an indirect function call.
Chris Lattnerbe1987772005-12-22 06:07:52 +0000172 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
173 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
174 II != IE; ++II) {
Chris Lattner9c4a58b2003-10-31 21:05:12 +0000175 CallSite CS = CallSite::get(II);
176 if (CS.getInstruction() && !CS.getCalledFunction())
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000177 Node->addCalledFunction(CS, CallsExternalNode);
Chris Lattnerbe1987772005-12-22 06:07:52 +0000178 }
179 }
180
181 //
182 // destroy - Release memory for the call graph
183 virtual void destroy() {
Chris Lattnerdd23d3d2006-12-05 19:46:12 +0000184 /// CallsExternalNode is not in the function map, delete it explicitly.
Chris Lattnerbaf6c542006-12-04 21:22:45 +0000185 delete CallsExternalNode;
186 CallsExternalNode = 0;
Chris Lattnerdd23d3d2006-12-05 19:46:12 +0000187 CallGraph::destroy();
Chris Lattnerbe1987772005-12-22 06:07:52 +0000188 }
189};
Chris Lattnerbbf3ae82001-09-28 00:08:15 +0000190
Chris Lattnerbe1987772005-12-22 06:07:52 +0000191} //End anonymous namespace
192
Dan Gohmand78c4002008-05-13 00:00:25 +0000193static RegisterAnalysisGroup<CallGraph> X("Call Graph");
194static RegisterPass<BasicCallGraph>
195Y("basiccg", "Basic CallGraph Construction", false, true);
196static RegisterAnalysisGroup<CallGraph, true> Z(Y);
197
Devang Patel8c78a0b2007-05-03 01:11:54 +0000198char CallGraph::ID = 0;
199char BasicCallGraph::ID = 0;
Lauro Ramos Venancio41223582007-05-02 20:37:47 +0000200
Chris Lattnerbe1987772005-12-22 06:07:52 +0000201void CallGraph::initialize(Module &M) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000202 Mod = &M;
Chris Lattnerbbf3ae82001-09-28 00:08:15 +0000203}
204
Chris Lattner80327322002-03-06 17:16:43 +0000205void CallGraph::destroy() {
Chris Lattneraba5e1e2006-10-09 17:28:13 +0000206 if (!FunctionMap.empty()) {
Chris Lattnerbe1987772005-12-22 06:07:52 +0000207 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
208 I != E; ++I)
209 delete I->second;
210 FunctionMap.clear();
211 }
Chris Lattner03946cd2001-11-26 18:51:25 +0000212}
213
Chris Lattner6b5411022004-08-08 03:27:49 +0000214void CallGraph::print(std::ostream &OS, const Module *M) const {
Chris Lattnerb9d55472002-11-04 00:21:19 +0000215 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner6b5411022004-08-08 03:27:49 +0000216 I->second->print(OS);
217}
218
Chris Lattnerbc351e12006-01-14 19:17:02 +0000219void CallGraph::dump() const {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000220 print(cerr, 0);
Chris Lattnerbc351e12006-01-14 19:17:02 +0000221}
222
Chris Lattner03946cd2001-11-26 18:51:25 +0000223//===----------------------------------------------------------------------===//
224// Implementations of public modification methods
225//
226
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000227// removeFunctionFromModule - Unlink the function from this module, returning
228// it. Because this removes the function from the module, the call graph node
229// is destroyed. This is only valid if the function does not call any other
230// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner03946cd2001-11-26 18:51:25 +0000231// is to dropAllReferences before calling this.
232//
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000233Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
234 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
235 "graph if it references other functions!");
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000236 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000237 delete CGN; // Delete the call graph node for this func
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000238 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner03946cd2001-11-26 18:51:25 +0000239
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000240 Mod->getFunctionList().remove(F);
241 return F;
Chris Lattner03946cd2001-11-26 18:51:25 +0000242}
243
Chris Lattnere81c2aa2004-09-18 00:22:13 +0000244// changeFunction - This method changes the function associated with this
245// CallGraphNode, for use by transformations that need to change the prototype
246// of a Function (thus they must create a new Function and move the old code
247// over).
248void CallGraph::changeFunction(Function *OldF, Function *NewF) {
249 iterator I = FunctionMap.find(OldF);
250 CallGraphNode *&New = FunctionMap[NewF];
251 assert(I != FunctionMap.end() && I->second && !New &&
252 "OldF didn't exist in CG or NewF already does!");
253 New = I->second;
Chris Lattner85d5ccc2004-09-18 00:27:20 +0000254 New->F = NewF;
Chris Lattnere81c2aa2004-09-18 00:22:13 +0000255 FunctionMap.erase(I);
256}
257
Chris Lattner00ca8d22006-01-14 20:03:00 +0000258// getOrInsertFunction - This method is identical to calling operator[], but
259// it will insert a new CallGraphNode for the specified function if one does
260// not already exist.
261CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
262 CallGraphNode *&CGN = FunctionMap[F];
263 if (CGN) return CGN;
264
265 assert((!F || F->getParent() == Mod) && "Function not in current module!");
266 return CGN = new CallGraphNode(const_cast<Function*>(F));
267}
268
Chris Lattnerbe1987772005-12-22 06:07:52 +0000269void CallGraphNode::print(std::ostream &OS) const {
270 if (Function *F = getFunction())
271 OS << "Call graph node for function: '" << F->getName() <<"'\n";
272 else
273 OS << "Call graph node <<null function: 0x" << this << ">>:\n";
274
275 for (const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000276 if (I->second->getFunction())
277 OS << " Calls function '" << I->second->getFunction()->getName() <<"'\n";
Chris Lattnerbe1987772005-12-22 06:07:52 +0000278 else
279 OS << " Calls external node\n";
280 OS << "\n";
281}
282
Bill Wendlingf3baad32006-12-07 01:30:32 +0000283void CallGraphNode::dump() const { print(cerr); }
Chris Lattnerbe1987772005-12-22 06:07:52 +0000284
Chris Lattner8b6db182004-04-12 05:36:32 +0000285void CallGraphNode::removeCallEdgeTo(CallGraphNode *Callee) {
286 for (unsigned i = CalledFunctions.size(); ; --i) {
287 assert(i && "Cannot find callee to remove!");
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000288 if (CalledFunctions[i-1].second == Callee) {
Chris Lattner8b6db182004-04-12 05:36:32 +0000289 CalledFunctions.erase(CalledFunctions.begin()+i-1);
290 return;
291 }
292 }
293}
Chris Lattner824a2182004-09-18 21:34:34 +0000294
Chris Lattnercc9709c2008-04-13 19:41:25 +0000295/// removeCallEdgeFor - This method removes the edge in the node for the
296/// specified call site. Note that this method takes linear time, so it
297/// should be used sparingly.
298void CallGraphNode::removeCallEdgeFor(CallSite CS) {
299 for (unsigned i = CalledFunctions.size(); ; --i) {
300 assert(i && "Cannot find callee to remove!");
301 if (CalledFunctions[i-1].first == CS) {
302 CalledFunctions.erase(CalledFunctions.begin()+i-1);
303 return;
304 }
305 }
306}
307
308
Chris Lattner824a2182004-09-18 21:34:34 +0000309// removeAnyCallEdgeTo - This method removes any call edges from this node to
310// the specified callee function. This takes more time to execute than
311// removeCallEdgeTo, so it should not be used unless necessary.
312void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
Chris Lattnerd6d99df2004-09-19 19:01:06 +0000313 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000314 if (CalledFunctions[i].second == Callee) {
Chris Lattnerd6d99df2004-09-19 19:01:06 +0000315 CalledFunctions[i] = CalledFunctions.back();
316 CalledFunctions.pop_back();
317 --i; --e;
Chris Lattner824a2182004-09-18 21:34:34 +0000318 }
319}
Reid Spencerbe535662006-06-07 22:00:26 +0000320
321// Enuse that users of CallGraph.h also link with this file
322DEFINING_FILE_FOR(CallGraph)