blob: 5f9850c93dcbff08dd43d71558e6eed0fec6971a [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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 Lattner12d38bf2006-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 Lattner07a38e72003-10-31 21:05:12 +000027static bool isOnlyADirectCall(Function *F, CallSite CS) {
28 if (!CS.getInstruction()) return false;
29 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; ++I)
30 if (*I == F) return false;
31 return true;
32}
33
Chris Lattner03839952005-12-22 06:07:52 +000034namespace {
35
36//===----------------------------------------------------------------------===//
37// BasicCallGraph class definition
Chris Lattner41fbf302001-09-28 00:08:15 +000038//
Reid Spencerd7d83db2007-02-05 23:42:17 +000039class VISIBILITY_HIDDEN BasicCallGraph : public CallGraph, public ModulePass {
Chris Lattner03839952005-12-22 06:07:52 +000040 // Root is root of the call graph, or the external node if a 'main' function
41 // couldn't be found.
42 //
43 CallGraphNode *Root;
Chris Lattner41fbf302001-09-28 00:08:15 +000044
Chris Lattner03839952005-12-22 06:07:52 +000045 // ExternalCallingNode - This node has edges to all external functions and
46 // those internal functions that have their address taken.
47 CallGraphNode *ExternalCallingNode;
Chris Lattner25e9cad2001-11-26 18:51:25 +000048
Chris Lattner03839952005-12-22 06:07:52 +000049 // CallsExternalNode - This node has edges to it from all functions making
50 // indirect calls or calling an external function.
51 CallGraphNode *CallsExternalNode;
52
53public:
Devang Patel19974732007-05-03 01:11:54 +000054 static char ID; // Class identification, replacement for typeinfo
Devang Patel794fd752007-05-01 21:15:47 +000055 BasicCallGraph() : ModulePass((intptr_t)&ID), Root(0),
56 ExternalCallingNode(0), CallsExternalNode(0) {}
Chris Lattner03839952005-12-22 06:07:52 +000057
58 // runOnModule - Compute the call graph for the specified module.
59 virtual bool runOnModule(Module &M) {
Chris Lattner03839952005-12-22 06:07:52 +000060 CallGraph::initialize(M);
61
Chris Lattnerc54b1c12006-01-14 20:03:00 +000062 ExternalCallingNode = getOrInsertFunction(0);
Chris Lattner03839952005-12-22 06:07:52 +000063 CallsExternalNode = new CallGraphNode(0);
64 Root = 0;
65
66 // Add every function to the call graph...
67 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
68 addToCallGraph(I);
69
70 // If we didn't find a main function, use the external call graph node
71 if (Root == 0) Root = ExternalCallingNode;
72
73 return false;
Chris Lattnerd4d427b2002-03-06 20:19:35 +000074 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000075
Chris Lattner03839952005-12-22 06:07:52 +000076 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
77 AU.setPreservesAll();
78 }
Chris Lattnerd4d427b2002-03-06 20:19:35 +000079
Bill Wendling5c7e3262006-12-17 05:15:13 +000080 void print(std::ostream *o, const Module *M) const {
81 if (o) print(*o, M);
Bill Wendling68fe61d2006-11-29 00:19:40 +000082 }
83
Chris Lattner03839952005-12-22 06:07:52 +000084 virtual void print(std::ostream &o, const Module *M) const {
85 o << "CallGraph Root is: ";
86 if (Function *F = getRoot()->getFunction())
87 o << F->getName() << "\n";
88 else
89 o << "<<null function: 0x" << getRoot() << ">>\n";
90
91 CallGraph::print(o, M);
92 }
93
94 virtual void releaseMemory() {
95 destroy();
96 }
97
98 /// dump - Print out this call graph.
99 ///
100 inline void dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000101 print(cerr, Mod);
Chris Lattner03839952005-12-22 06:07:52 +0000102 }
103
104 CallGraphNode* getExternalCallingNode() const { return ExternalCallingNode; }
105 CallGraphNode* getCallsExternalNode() const { return CallsExternalNode; }
106
107 // getRoot - Return the root of the call graph, which is either main, or if
108 // main cannot be found, the external node.
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000109 //
Chris Lattner03839952005-12-22 06:07:52 +0000110 CallGraphNode *getRoot() { return Root; }
111 const CallGraphNode *getRoot() const { return Root; }
112
113private:
114 //===---------------------------------------------------------------------
115 // Implementation of CallGraph construction
116 //
Chris Lattner03839952005-12-22 06:07:52 +0000117
Chris Lattner03839952005-12-22 06:07:52 +0000118 // addToCallGraph - Add a function to the call graph, and link the node to all
119 // of the functions that it calls.
120 //
121 void addToCallGraph(Function *F) {
Chris Lattnerc54b1c12006-01-14 20:03:00 +0000122 CallGraphNode *Node = getOrInsertFunction(F);
Chris Lattner03839952005-12-22 06:07:52 +0000123
Chris Lattnerd85340f2006-07-12 18:29:36 +0000124 // If this function has external linkage, anything could call it.
Chris Lattner03839952005-12-22 06:07:52 +0000125 if (!F->hasInternalLinkage()) {
Chris Lattnerd85340f2006-07-12 18:29:36 +0000126 ExternalCallingNode->addCalledFunction(CallSite(), Node);
Chris Lattner03839952005-12-22 06:07:52 +0000127
128 // Found the entry point?
129 if (F->getName() == "main") {
130 if (Root) // Found multiple external mains? Don't pick one.
131 Root = ExternalCallingNode;
132 else
133 Root = Node; // Found a main, keep track of it!
134 }
135 }
136
137 // If this function is not defined in this translation unit, it could call
138 // anything.
Reid Spencer5cbf9852007-01-30 20:08:39 +0000139 if (F->isDeclaration() && !F->getIntrinsicID())
Chris Lattnerd85340f2006-07-12 18:29:36 +0000140 Node->addCalledFunction(CallSite(), CallsExternalNode);
Chris Lattner03839952005-12-22 06:07:52 +0000141
142 // Loop over all of the users of the function... looking for callers...
143 //
144 bool isUsedExternally = false;
145 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I){
146 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
Chris Lattnerd85340f2006-07-12 18:29:36 +0000147 CallSite CS = CallSite::get(Inst);
148 if (isOnlyADirectCall(F, CS))
Chris Lattnerc54b1c12006-01-14 20:03:00 +0000149 getOrInsertFunction(Inst->getParent()->getParent())
Chris Lattnerd85340f2006-07-12 18:29:36 +0000150 ->addCalledFunction(CS, Node);
Chris Lattner03839952005-12-22 06:07:52 +0000151 else
152 isUsedExternally = true;
153 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(*I)) {
154 for (Value::use_iterator I = GV->use_begin(), E = GV->use_end();
155 I != E; ++I)
156 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
Chris Lattnerd85340f2006-07-12 18:29:36 +0000157 CallSite CS = CallSite::get(Inst);
158 if (isOnlyADirectCall(F, CS))
159 getOrInsertFunction(Inst->getParent()->getParent())
160 ->addCalledFunction(CS, Node);
161 else
162 isUsedExternally = true;
Chris Lattner03839952005-12-22 06:07:52 +0000163 } else {
164 isUsedExternally = true;
165 }
166 } else { // Can't classify the user!
167 isUsedExternally = true;
168 }
Chris Lattner07a38e72003-10-31 21:05:12 +0000169 }
Chris Lattner03839952005-12-22 06:07:52 +0000170 if (isUsedExternally)
Chris Lattnerd85340f2006-07-12 18:29:36 +0000171 ExternalCallingNode->addCalledFunction(CallSite(), Node);
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000172
Chris Lattnerd85340f2006-07-12 18:29:36 +0000173 // Look for an indirect function call.
Chris Lattner03839952005-12-22 06:07:52 +0000174 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
175 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
176 II != IE; ++II) {
Chris Lattner07a38e72003-10-31 21:05:12 +0000177 CallSite CS = CallSite::get(II);
178 if (CS.getInstruction() && !CS.getCalledFunction())
Chris Lattnerd85340f2006-07-12 18:29:36 +0000179 Node->addCalledFunction(CS, CallsExternalNode);
Chris Lattner03839952005-12-22 06:07:52 +0000180 }
181 }
182
183 //
184 // destroy - Release memory for the call graph
185 virtual void destroy() {
Chris Lattnerf2d9ceb2006-12-05 19:46:12 +0000186 /// CallsExternalNode is not in the function map, delete it explicitly.
Chris Lattner12d38bf2006-12-04 21:22:45 +0000187 delete CallsExternalNode;
188 CallsExternalNode = 0;
Chris Lattnerf2d9ceb2006-12-05 19:46:12 +0000189 CallGraph::destroy();
Chris Lattner03839952005-12-22 06:07:52 +0000190 }
191};
Chris Lattner41fbf302001-09-28 00:08:15 +0000192
Chris Lattner03839952005-12-22 06:07:52 +0000193RegisterAnalysisGroup<CallGraph> X("Call Graph");
Chris Lattner7f8897f2006-08-27 22:42:52 +0000194RegisterPass<BasicCallGraph> Y("basiccg", "Basic CallGraph Construction");
Chris Lattnera5370172006-08-28 00:42:29 +0000195RegisterAnalysisGroup<CallGraph, true> Z(Y);
Chris Lattner03839952005-12-22 06:07:52 +0000196
197} //End anonymous namespace
198
Devang Patel19974732007-05-03 01:11:54 +0000199char CallGraph::ID = 0;
200char BasicCallGraph::ID = 0;
Lauro Ramos Venancioc7182882007-05-02 20:37:47 +0000201
Chris Lattner03839952005-12-22 06:07:52 +0000202void CallGraph::initialize(Module &M) {
Chris Lattner7e708292002-06-25 16:13:24 +0000203 Mod = &M;
Chris Lattner41fbf302001-09-28 00:08:15 +0000204}
205
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000206void CallGraph::destroy() {
Chris Lattner45d10472006-10-09 17:28:13 +0000207 if (!FunctionMap.empty()) {
Chris Lattner03839952005-12-22 06:07:52 +0000208 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
209 I != E; ++I)
210 delete I->second;
211 FunctionMap.clear();
212 }
Chris Lattner25e9cad2001-11-26 18:51:25 +0000213}
214
Chris Lattneraf8a4242004-08-08 03:27:49 +0000215void CallGraph::print(std::ostream &OS, const Module *M) const {
Chris Lattner16500152002-11-04 00:21:19 +0000216 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattneraf8a4242004-08-08 03:27:49 +0000217 I->second->print(OS);
218}
219
Chris Lattner1694ec62006-01-14 19:17:02 +0000220void CallGraph::dump() const {
Bill Wendlinge8156192006-12-07 01:30:32 +0000221 print(cerr, 0);
Chris Lattner1694ec62006-01-14 19:17:02 +0000222}
223
Chris Lattner25e9cad2001-11-26 18:51:25 +0000224//===----------------------------------------------------------------------===//
225// Implementations of public modification methods
226//
227
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000228// removeFunctionFromModule - Unlink the function from this module, returning
229// it. Because this removes the function from the module, the call graph node
230// is destroyed. This is only valid if the function does not call any other
231// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner25e9cad2001-11-26 18:51:25 +0000232// is to dropAllReferences before calling this.
233//
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000234Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
235 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
236 "graph if it references other functions!");
Chris Lattner5714c972003-08-31 20:36:52 +0000237 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000238 delete CGN; // Delete the call graph node for this func
Chris Lattner5714c972003-08-31 20:36:52 +0000239 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner25e9cad2001-11-26 18:51:25 +0000240
Chris Lattner5714c972003-08-31 20:36:52 +0000241 Mod->getFunctionList().remove(F);
242 return F;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000243}
244
Chris Lattner6f7e5eb2004-09-18 00:22:13 +0000245// changeFunction - This method changes the function associated with this
246// CallGraphNode, for use by transformations that need to change the prototype
247// of a Function (thus they must create a new Function and move the old code
248// over).
249void CallGraph::changeFunction(Function *OldF, Function *NewF) {
250 iterator I = FunctionMap.find(OldF);
251 CallGraphNode *&New = FunctionMap[NewF];
252 assert(I != FunctionMap.end() && I->second && !New &&
253 "OldF didn't exist in CG or NewF already does!");
254 New = I->second;
Chris Lattner3795bc92004-09-18 00:27:20 +0000255 New->F = NewF;
Chris Lattner6f7e5eb2004-09-18 00:22:13 +0000256 FunctionMap.erase(I);
257}
258
Chris Lattnerc54b1c12006-01-14 20:03:00 +0000259// getOrInsertFunction - This method is identical to calling operator[], but
260// it will insert a new CallGraphNode for the specified function if one does
261// not already exist.
262CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
263 CallGraphNode *&CGN = FunctionMap[F];
264 if (CGN) return CGN;
265
266 assert((!F || F->getParent() == Mod) && "Function not in current module!");
267 return CGN = new CallGraphNode(const_cast<Function*>(F));
268}
269
Chris Lattner03839952005-12-22 06:07:52 +0000270void CallGraphNode::print(std::ostream &OS) const {
271 if (Function *F = getFunction())
272 OS << "Call graph node for function: '" << F->getName() <<"'\n";
273 else
274 OS << "Call graph node <<null function: 0x" << this << ">>:\n";
275
276 for (const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerd85340f2006-07-12 18:29:36 +0000277 if (I->second->getFunction())
278 OS << " Calls function '" << I->second->getFunction()->getName() <<"'\n";
Chris Lattner03839952005-12-22 06:07:52 +0000279 else
280 OS << " Calls external node\n";
281 OS << "\n";
282}
283
Bill Wendlinge8156192006-12-07 01:30:32 +0000284void CallGraphNode::dump() const { print(cerr); }
Chris Lattner03839952005-12-22 06:07:52 +0000285
Chris Lattnerb81c0212004-04-12 05:36:32 +0000286void CallGraphNode::removeCallEdgeTo(CallGraphNode *Callee) {
287 for (unsigned i = CalledFunctions.size(); ; --i) {
288 assert(i && "Cannot find callee to remove!");
Chris Lattnerd85340f2006-07-12 18:29:36 +0000289 if (CalledFunctions[i-1].second == Callee) {
Chris Lattnerb81c0212004-04-12 05:36:32 +0000290 CalledFunctions.erase(CalledFunctions.begin()+i-1);
291 return;
292 }
293 }
294}
Chris Lattnercd382a32004-09-18 21:34:34 +0000295
296// removeAnyCallEdgeTo - This method removes any call edges from this node to
297// the specified callee function. This takes more time to execute than
298// removeCallEdgeTo, so it should not be used unless necessary.
299void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
Chris Lattnerfff03c92004-09-19 19:01:06 +0000300 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
Chris Lattnerd85340f2006-07-12 18:29:36 +0000301 if (CalledFunctions[i].second == Callee) {
Chris Lattnerfff03c92004-09-19 19:01:06 +0000302 CalledFunctions[i] = CalledFunctions.back();
303 CalledFunctions.pop_back();
304 --i; --e;
Chris Lattnercd382a32004-09-18 21:34:34 +0000305 }
306}
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000307
308// Enuse that users of CallGraph.h also link with this file
309DEFINING_FILE_FOR(CallGraph)