blob: 1a65179c5aac56972a9a8cb0a42939d9a0c93f95 [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
127 // If this function is not defined in this translation unit, it could call
128 // anything.
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000129 if (F->isDeclaration() && !F->isIntrinsic())
Chris Lattnerd85340f2006-07-12 18:29:36 +0000130 Node->addCalledFunction(CallSite(), CallsExternalNode);
Chris Lattner03839952005-12-22 06:07:52 +0000131
Duncan Sands99c1a7c2008-09-09 12:40:47 +0000132 // Loop over all of the users of the function, looking for non-call uses.
Chris Lattner03839952005-12-22 06:07:52 +0000133 bool isUsedExternally = false;
Duncan Sands99c1a7c2008-09-09 12:40:47 +0000134 for (Value::use_iterator I = F->use_begin(), E = F->use_end();
135 I != E && !isUsedExternally; ++I) {
Chris Lattner03839952005-12-22 06:07:52 +0000136 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
Chris Lattnerd85340f2006-07-12 18:29:36 +0000137 CallSite CS = CallSite::get(Inst);
Duncan Sands24a05212008-09-09 13:44:24 +0000138 // Not a call? Or F being passed as a parameter not as the callee?
139 isUsedExternally = !CS.getInstruction() || I.getOperandNo();
Duncan Sands99c1a7c2008-09-09 12:40:47 +0000140 } else { // User is not a direct call!
Chris Lattner03839952005-12-22 06:07:52 +0000141 isUsedExternally = true;
142 }
Chris Lattner07a38e72003-10-31 21:05:12 +0000143 }
Chris Lattner03839952005-12-22 06:07:52 +0000144 if (isUsedExternally)
Chris Lattnerd85340f2006-07-12 18:29:36 +0000145 ExternalCallingNode->addCalledFunction(CallSite(), Node);
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000146
Duncan Sands99c1a7c2008-09-09 12:40:47 +0000147 // Look for calls by this function.
Chris Lattner03839952005-12-22 06:07:52 +0000148 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
149 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
150 II != IE; ++II) {
Duncan Sands99c1a7c2008-09-09 12:40:47 +0000151 CallSite CS = CallSite::get(II);
152 if (CS.getInstruction()) {
153 const Function *Callee = CS.getCalledFunction();
154 if (Callee)
155 Node->addCalledFunction(CS, getOrInsertFunction(Callee));
156 else
157 Node->addCalledFunction(CS, CallsExternalNode);
158 }
Chris Lattner03839952005-12-22 06:07:52 +0000159 }
160 }
161
162 //
163 // destroy - Release memory for the call graph
164 virtual void destroy() {
Chris Lattnerf2d9ceb2006-12-05 19:46:12 +0000165 /// CallsExternalNode is not in the function map, delete it explicitly.
Chris Lattner12d38bf2006-12-04 21:22:45 +0000166 delete CallsExternalNode;
167 CallsExternalNode = 0;
Chris Lattnerf2d9ceb2006-12-05 19:46:12 +0000168 CallGraph::destroy();
Chris Lattner03839952005-12-22 06:07:52 +0000169 }
170};
Chris Lattner41fbf302001-09-28 00:08:15 +0000171
Chris Lattner03839952005-12-22 06:07:52 +0000172} //End anonymous namespace
173
Dan Gohman844731a2008-05-13 00:00:25 +0000174static RegisterAnalysisGroup<CallGraph> X("Call Graph");
175static RegisterPass<BasicCallGraph>
176Y("basiccg", "Basic CallGraph Construction", false, true);
177static RegisterAnalysisGroup<CallGraph, true> Z(Y);
178
Devang Patel19974732007-05-03 01:11:54 +0000179char CallGraph::ID = 0;
180char BasicCallGraph::ID = 0;
Lauro Ramos Venancioc7182882007-05-02 20:37:47 +0000181
Chris Lattner03839952005-12-22 06:07:52 +0000182void CallGraph::initialize(Module &M) {
Chris Lattner7e708292002-06-25 16:13:24 +0000183 Mod = &M;
Chris Lattner41fbf302001-09-28 00:08:15 +0000184}
185
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000186void CallGraph::destroy() {
Chris Lattner45d10472006-10-09 17:28:13 +0000187 if (!FunctionMap.empty()) {
Chris Lattner03839952005-12-22 06:07:52 +0000188 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
189 I != E; ++I)
190 delete I->second;
191 FunctionMap.clear();
192 }
Chris Lattner25e9cad2001-11-26 18:51:25 +0000193}
194
Chris Lattneraf8a4242004-08-08 03:27:49 +0000195void CallGraph::print(std::ostream &OS, const Module *M) const {
Chris Lattner16500152002-11-04 00:21:19 +0000196 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattneraf8a4242004-08-08 03:27:49 +0000197 I->second->print(OS);
198}
199
Chris Lattner1694ec62006-01-14 19:17:02 +0000200void CallGraph::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000201 print(cerr, 0);
Chris Lattner1694ec62006-01-14 19:17:02 +0000202}
203
Chris Lattner25e9cad2001-11-26 18:51:25 +0000204//===----------------------------------------------------------------------===//
205// Implementations of public modification methods
206//
207
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000208// removeFunctionFromModule - Unlink the function from this module, returning
209// it. Because this removes the function from the module, the call graph node
210// is destroyed. This is only valid if the function does not call any other
211// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner25e9cad2001-11-26 18:51:25 +0000212// is to dropAllReferences before calling this.
213//
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000214Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
215 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
216 "graph if it references other functions!");
Chris Lattner5714c972003-08-31 20:36:52 +0000217 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000218 delete CGN; // Delete the call graph node for this func
Chris Lattner5714c972003-08-31 20:36:52 +0000219 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner25e9cad2001-11-26 18:51:25 +0000220
Chris Lattner5714c972003-08-31 20:36:52 +0000221 Mod->getFunctionList().remove(F);
222 return F;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000223}
224
Chris Lattner6f7e5eb2004-09-18 00:22:13 +0000225// changeFunction - This method changes the function associated with this
226// CallGraphNode, for use by transformations that need to change the prototype
227// of a Function (thus they must create a new Function and move the old code
228// over).
229void CallGraph::changeFunction(Function *OldF, Function *NewF) {
230 iterator I = FunctionMap.find(OldF);
231 CallGraphNode *&New = FunctionMap[NewF];
232 assert(I != FunctionMap.end() && I->second && !New &&
233 "OldF didn't exist in CG or NewF already does!");
234 New = I->second;
Chris Lattner3795bc92004-09-18 00:27:20 +0000235 New->F = NewF;
Chris Lattner6f7e5eb2004-09-18 00:22:13 +0000236 FunctionMap.erase(I);
237}
238
Chris Lattnerc54b1c12006-01-14 20:03:00 +0000239// getOrInsertFunction - This method is identical to calling operator[], but
240// it will insert a new CallGraphNode for the specified function if one does
241// not already exist.
242CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
243 CallGraphNode *&CGN = FunctionMap[F];
244 if (CGN) return CGN;
245
246 assert((!F || F->getParent() == Mod) && "Function not in current module!");
247 return CGN = new CallGraphNode(const_cast<Function*>(F));
248}
249
Chris Lattner03839952005-12-22 06:07:52 +0000250void CallGraphNode::print(std::ostream &OS) const {
251 if (Function *F = getFunction())
252 OS << "Call graph node for function: '" << F->getName() <<"'\n";
253 else
254 OS << "Call graph node <<null function: 0x" << this << ">>:\n";
255
Duncan Sands28f02122008-09-08 16:04:03 +0000256 for (const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerd85340f2006-07-12 18:29:36 +0000257 if (I->second->getFunction())
Duncan Sands28f02122008-09-08 16:04:03 +0000258 OS << " Calls function '" << I->second->getFunction()->getName() <<"'\n";
259 else
260 OS << " Calls external node\n";
Chris Lattner03839952005-12-22 06:07:52 +0000261 OS << "\n";
262}
263
Bill Wendlinge8156192006-12-07 01:30:32 +0000264void CallGraphNode::dump() const { print(cerr); }
Chris Lattner03839952005-12-22 06:07:52 +0000265
Chris Lattner75caee22008-04-13 19:41:25 +0000266/// removeCallEdgeFor - This method removes the edge in the node for the
267/// specified call site. Note that this method takes linear time, so it
268/// should be used sparingly.
269void CallGraphNode::removeCallEdgeFor(CallSite CS) {
270 for (unsigned i = CalledFunctions.size(); ; --i) {
Duncan Sandsfec2c2b2008-09-06 17:19:29 +0000271 assert(i && "Cannot find callsite to remove!");
Chris Lattner75caee22008-04-13 19:41:25 +0000272 if (CalledFunctions[i-1].first == CS) {
273 CalledFunctions.erase(CalledFunctions.begin()+i-1);
274 return;
275 }
276 }
277}
278
279
Chris Lattnercd382a32004-09-18 21:34:34 +0000280// removeAnyCallEdgeTo - This method removes any call edges from this node to
281// the specified callee function. This takes more time to execute than
282// removeCallEdgeTo, so it should not be used unless necessary.
283void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
Chris Lattnerfff03c92004-09-19 19:01:06 +0000284 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
Chris Lattnerd85340f2006-07-12 18:29:36 +0000285 if (CalledFunctions[i].second == Callee) {
Chris Lattnerfff03c92004-09-19 19:01:06 +0000286 CalledFunctions[i] = CalledFunctions.back();
287 CalledFunctions.pop_back();
288 --i; --e;
Chris Lattnercd382a32004-09-18 21:34:34 +0000289 }
290}
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000291
Duncan Sandsfec2c2b2008-09-06 17:19:29 +0000292/// replaceCallSite - Make the edge in the node for Old CallSite be for
293/// New CallSite instead. Note that this method takes linear time, so it
294/// should be used sparingly.
295void CallGraphNode::replaceCallSite(CallSite Old, CallSite New) {
296 for (unsigned i = CalledFunctions.size(); ; --i) {
297 assert(i && "Cannot find callsite to replace!");
298 if (CalledFunctions[i-1].first == Old) {
299 CalledFunctions[i-1].first = New;
300 return;
301 }
302 }
303}
304
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000305// Enuse that users of CallGraph.h also link with this file
306DEFINING_FILE_FOR(CallGraph)