blob: ea7d7feb4cd83000e520c06f7f6b93345e7dfdff [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- CallGraph.cpp - Build a Module's call graph ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the CallGraph class and provides the BasicCallGraph
11// default implementation.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/CallGraph.h"
16#include "llvm/Module.h"
17#include "llvm/Instructions.h"
18#include "llvm/Support/CallSite.h"
19#include "llvm/Support/Compiler.h"
20#include "llvm/Support/Streams.h"
21#include <ostream>
22using namespace llvm;
23
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024namespace {
25
26//===----------------------------------------------------------------------===//
27// BasicCallGraph class definition
28//
29class VISIBILITY_HIDDEN BasicCallGraph : public CallGraph, public ModulePass {
30 // Root is root of the call graph, or the external node if a 'main' function
31 // couldn't be found.
32 //
33 CallGraphNode *Root;
34
35 // ExternalCallingNode - This node has edges to all external functions and
36 // those internal functions that have their address taken.
37 CallGraphNode *ExternalCallingNode;
38
39 // CallsExternalNode - This node has edges to it from all functions making
40 // indirect calls or calling an external function.
41 CallGraphNode *CallsExternalNode;
42
43public:
44 static char ID; // Class identification, replacement for typeinfo
Dan Gohman26f8c272008-09-04 17:05:41 +000045 BasicCallGraph() : ModulePass(&ID), Root(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 ExternalCallingNode(0), CallsExternalNode(0) {}
47
48 // runOnModule - Compute the call graph for the specified module.
49 virtual bool runOnModule(Module &M) {
50 CallGraph::initialize(M);
51
52 ExternalCallingNode = getOrInsertFunction(0);
53 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;
64 }
65
66 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
67 AU.setPreservesAll();
68 }
69
70 void print(std::ostream *o, const Module *M) const {
71 if (o) print(*o, M);
72 }
73
74 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 {
91 print(cerr, Mod);
92 }
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.
99 //
100 CallGraphNode *getRoot() { return Root; }
101 const CallGraphNode *getRoot() const { return Root; }
102
103private:
104 //===---------------------------------------------------------------------
105 // Implementation of CallGraph construction
106 //
107
108 // 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) {
112 CallGraphNode *Node = getOrInsertFunction(F);
113
114 // If this function has external linkage, anything could call it.
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000115 if (!F->hasLocalLinkage()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 ExternalCallingNode->addCalledFunction(CallSite(), Node);
117
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 Sands3ebfa912008-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)
Gabor Greif6be9a1b2009-01-22 21:35:57 +0000129 if ((!isa<CallInst>(I) && !isa<InvokeInst>(I))
130 || !CallSite(cast<Instruction>(I)).isCallee(I)) {
Duncan Sands3ebfa912008-09-09 19:56:34 +0000131 // Not a call, or being used as a parameter rather than as the callee.
132 ExternalCallingNode->addCalledFunction(CallSite(), Node);
133 break;
134 }
135
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 // If this function is not defined in this translation unit, it could call
137 // anything.
Duncan Sands79d28872007-12-03 20:06:50 +0000138 if (F->isDeclaration() && !F->isIntrinsic())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 Node->addCalledFunction(CallSite(), CallsExternalNode);
140
Duncan Sands8f7eaea2008-09-09 12:40:47 +0000141 // Look for calls by this function.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
143 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
144 II != IE; ++II) {
Duncan Sands8f7eaea2008-09-09 12:40:47 +0000145 CallSite CS = CallSite::get(II);
146 if (CS.getInstruction()) {
147 const Function *Callee = CS.getCalledFunction();
148 if (Callee)
149 Node->addCalledFunction(CS, getOrInsertFunction(Callee));
150 else
151 Node->addCalledFunction(CS, CallsExternalNode);
152 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 }
154 }
155
156 //
157 // destroy - Release memory for the call graph
158 virtual void destroy() {
159 /// CallsExternalNode is not in the function map, delete it explicitly.
160 delete CallsExternalNode;
161 CallsExternalNode = 0;
162 CallGraph::destroy();
163 }
164};
165
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166} //End anonymous namespace
167
Dan Gohman089efff2008-05-13 00:00:25 +0000168static RegisterAnalysisGroup<CallGraph> X("Call Graph");
169static RegisterPass<BasicCallGraph>
170Y("basiccg", "Basic CallGraph Construction", false, true);
171static RegisterAnalysisGroup<CallGraph, true> Z(Y);
172
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173char CallGraph::ID = 0;
174char BasicCallGraph::ID = 0;
175
176void CallGraph::initialize(Module &M) {
177 Mod = &M;
178}
179
180void CallGraph::destroy() {
181 if (!FunctionMap.empty()) {
182 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
183 I != E; ++I)
184 delete I->second;
185 FunctionMap.clear();
186 }
187}
188
189void CallGraph::print(std::ostream &OS, const Module *M) const {
190 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
191 I->second->print(OS);
192}
193
194void CallGraph::dump() const {
195 print(cerr, 0);
196}
197
198//===----------------------------------------------------------------------===//
199// Implementations of public modification methods
200//
201
202// removeFunctionFromModule - Unlink the function from this module, returning
203// it. Because this removes the function from the module, the call graph node
204// is destroyed. This is only valid if the function does not call any other
205// functions (ie, there are no edges in it's CGN). The easiest way to do this
206// is to dropAllReferences before calling this.
207//
208Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
209 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
210 "graph if it references other functions!");
211 Function *F = CGN->getFunction(); // Get the function for the call graph node
212 delete CGN; // Delete the call graph node for this func
213 FunctionMap.erase(F); // Remove the call graph node from the map
214
215 Mod->getFunctionList().remove(F);
216 return F;
217}
218
219// changeFunction - This method changes the function associated with this
220// CallGraphNode, for use by transformations that need to change the prototype
221// of a Function (thus they must create a new Function and move the old code
222// over).
223void CallGraph::changeFunction(Function *OldF, Function *NewF) {
224 iterator I = FunctionMap.find(OldF);
225 CallGraphNode *&New = FunctionMap[NewF];
226 assert(I != FunctionMap.end() && I->second && !New &&
227 "OldF didn't exist in CG or NewF already does!");
228 New = I->second;
229 New->F = NewF;
230 FunctionMap.erase(I);
231}
232
233// getOrInsertFunction - This method is identical to calling operator[], but
234// it will insert a new CallGraphNode for the specified function if one does
235// not already exist.
236CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
237 CallGraphNode *&CGN = FunctionMap[F];
238 if (CGN) return CGN;
239
240 assert((!F || F->getParent() == Mod) && "Function not in current module!");
241 return CGN = new CallGraphNode(const_cast<Function*>(F));
242}
243
244void CallGraphNode::print(std::ostream &OS) const {
245 if (Function *F = getFunction())
246 OS << "Call graph node for function: '" << F->getName() <<"'\n";
247 else
248 OS << "Call graph node <<null function: 0x" << this << ">>:\n";
249
Duncan Sandsafeb3e82008-09-08 16:04:03 +0000250 for (const_iterator I = begin(), E = end(); I != E; ++I)
Gabor Greifbd78fa82009-01-14 17:09:04 +0000251 if (Function *FI = I->second->getFunction())
252 OS << " Calls function '" << FI->getName() <<"'\n";
Duncan Sandsafeb3e82008-09-08 16:04:03 +0000253 else
254 OS << " Calls external node\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 OS << "\n";
256}
257
258void CallGraphNode::dump() const { print(cerr); }
259
Chris Lattner9125c402008-04-13 19:41:25 +0000260/// removeCallEdgeFor - This method removes the edge in the node for the
261/// specified call site. Note that this method takes linear time, so it
262/// should be used sparingly.
263void CallGraphNode::removeCallEdgeFor(CallSite CS) {
Gabor Greif7819fbd2009-01-17 00:14:25 +0000264 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
265 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
266 if (I->first == CS) {
267 CalledFunctions.erase(I);
Chris Lattner9125c402008-04-13 19:41:25 +0000268 return;
269 }
270 }
271}
272
273
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274// removeAnyCallEdgeTo - This method removes any call edges from this node to
275// the specified callee function. This takes more time to execute than
276// removeCallEdgeTo, so it should not be used unless necessary.
277void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
278 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
279 if (CalledFunctions[i].second == Callee) {
280 CalledFunctions[i] = CalledFunctions.back();
281 CalledFunctions.pop_back();
282 --i; --e;
283 }
284}
285
Duncan Sands349bf2e2008-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 Greifa3ef1552009-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;
Duncan Sands349bf2e2008-10-03 07:36:09 +0000292 if (CR.second == Callee && !CR.first.getInstruction()) {
Gabor Greifa3ef1552009-01-17 19:46:01 +0000293 CalledFunctions.erase(I);
Duncan Sands349bf2e2008-10-03 07:36:09 +0000294 return;
295 }
296 }
297}
298
Duncan Sands7e8958a2008-09-06 17:19:29 +0000299/// replaceCallSite - Make the edge in the node for Old CallSite be for
300/// New CallSite instead. Note that this method takes linear time, so it
301/// should be used sparingly.
302void CallGraphNode::replaceCallSite(CallSite Old, CallSite New) {
Gabor Greifa3ef1552009-01-17 19:46:01 +0000303 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
304 assert(I != CalledFunctions.end() && "Cannot find callsite to replace!");
305 if (I->first == Old) {
306 I->first = New;
Duncan Sands7e8958a2008-09-06 17:19:29 +0000307 return;
308 }
309 }
310}
311
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312// Enuse that users of CallGraph.h also link with this file
313DEFINING_FILE_FOR(CallGraph)