blob: 128e11ebd7f28b3b46fa89098f4d466776ba4d80 [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"
Bill Wendling68fe61d2006-11-29 00:19:40 +000019#include "llvm/Support/Streams.h"
20#include <ostream>
Chris Lattnerb81c0212004-04-12 05:36:32 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Chris Lattner12d38bf2006-12-04 21:22:45 +000023/// isOnlyADirectCall - Return true if this callsite is *just* a direct call to
24/// the specified function. Specifically return false if the callsite also
25/// takes the address of the function.
Chris Lattner07a38e72003-10-31 21:05:12 +000026static bool isOnlyADirectCall(Function *F, CallSite CS) {
27 if (!CS.getInstruction()) return false;
28 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; ++I)
29 if (*I == F) return false;
30 return true;
31}
32
Chris Lattner03839952005-12-22 06:07:52 +000033namespace {
34
35//===----------------------------------------------------------------------===//
36// BasicCallGraph class definition
Chris Lattner41fbf302001-09-28 00:08:15 +000037//
Chris Lattner03839952005-12-22 06:07:52 +000038class BasicCallGraph : public CallGraph, public ModulePass {
39 // Root is root of the call graph, or the external node if a 'main' function
40 // couldn't be found.
41 //
42 CallGraphNode *Root;
Chris Lattner41fbf302001-09-28 00:08:15 +000043
Chris Lattner03839952005-12-22 06:07:52 +000044 // ExternalCallingNode - This node has edges to all external functions and
45 // those internal functions that have their address taken.
46 CallGraphNode *ExternalCallingNode;
Chris Lattner25e9cad2001-11-26 18:51:25 +000047
Chris Lattner03839952005-12-22 06:07:52 +000048 // CallsExternalNode - This node has edges to it from all functions making
49 // indirect calls or calling an external function.
50 CallGraphNode *CallsExternalNode;
51
52public:
53 BasicCallGraph() : Root(0), ExternalCallingNode(0), CallsExternalNode(0) {}
Chris Lattner03839952005-12-22 06:07:52 +000054
55 // runOnModule - Compute the call graph for the specified module.
56 virtual bool runOnModule(Module &M) {
Chris Lattner03839952005-12-22 06:07:52 +000057 CallGraph::initialize(M);
58
Chris Lattnerc54b1c12006-01-14 20:03:00 +000059 ExternalCallingNode = getOrInsertFunction(0);
Chris Lattner03839952005-12-22 06:07:52 +000060 CallsExternalNode = new CallGraphNode(0);
61 Root = 0;
62
63 // Add every function to the call graph...
64 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
65 addToCallGraph(I);
66
67 // If we didn't find a main function, use the external call graph node
68 if (Root == 0) Root = ExternalCallingNode;
69
70 return false;
Chris Lattnerd4d427b2002-03-06 20:19:35 +000071 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000072
Chris Lattner03839952005-12-22 06:07:52 +000073 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
74 AU.setPreservesAll();
75 }
Chris Lattnerd4d427b2002-03-06 20:19:35 +000076
Bill Wendling68fe61d2006-11-29 00:19:40 +000077 void print(llvm_ostream &o, const Module *M) const {
78 if (o.stream()) print(*o.stream(), M);
79 }
80
Chris Lattner03839952005-12-22 06:07:52 +000081 virtual void print(std::ostream &o, const Module *M) const {
82 o << "CallGraph Root is: ";
83 if (Function *F = getRoot()->getFunction())
84 o << F->getName() << "\n";
85 else
86 o << "<<null function: 0x" << getRoot() << ">>\n";
87
88 CallGraph::print(o, M);
89 }
90
91 virtual void releaseMemory() {
92 destroy();
93 }
94
95 /// dump - Print out this call graph.
96 ///
97 inline void dump() const {
Bill Wendling68fe61d2006-11-29 00:19:40 +000098 print(llvm_cerr, Mod);
Chris Lattner03839952005-12-22 06:07:52 +000099 }
100
101 CallGraphNode* getExternalCallingNode() const { return ExternalCallingNode; }
102 CallGraphNode* getCallsExternalNode() const { return CallsExternalNode; }
103
104 // getRoot - Return the root of the call graph, which is either main, or if
105 // main cannot be found, the external node.
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000106 //
Chris Lattner03839952005-12-22 06:07:52 +0000107 CallGraphNode *getRoot() { return Root; }
108 const CallGraphNode *getRoot() const { return Root; }
109
110private:
111 //===---------------------------------------------------------------------
112 // Implementation of CallGraph construction
113 //
Chris Lattner03839952005-12-22 06:07:52 +0000114
Chris Lattner03839952005-12-22 06:07:52 +0000115 // addToCallGraph - Add a function to the call graph, and link the node to all
116 // of the functions that it calls.
117 //
118 void addToCallGraph(Function *F) {
Chris Lattnerc54b1c12006-01-14 20:03:00 +0000119 CallGraphNode *Node = getOrInsertFunction(F);
Chris Lattner03839952005-12-22 06:07:52 +0000120
Chris Lattnerd85340f2006-07-12 18:29:36 +0000121 // If this function has external linkage, anything could call it.
Chris Lattner03839952005-12-22 06:07:52 +0000122 if (!F->hasInternalLinkage()) {
Chris Lattnerd85340f2006-07-12 18:29:36 +0000123 ExternalCallingNode->addCalledFunction(CallSite(), Node);
Chris Lattner03839952005-12-22 06:07:52 +0000124
125 // Found the entry point?
126 if (F->getName() == "main") {
127 if (Root) // Found multiple external mains? Don't pick one.
128 Root = ExternalCallingNode;
129 else
130 Root = Node; // Found a main, keep track of it!
131 }
132 }
133
134 // If this function is not defined in this translation unit, it could call
135 // anything.
136 if (F->isExternal() && !F->getIntrinsicID())
Chris Lattnerd85340f2006-07-12 18:29:36 +0000137 Node->addCalledFunction(CallSite(), CallsExternalNode);
Chris Lattner03839952005-12-22 06:07:52 +0000138
139 // Loop over all of the users of the function... looking for callers...
140 //
141 bool isUsedExternally = false;
142 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I){
143 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
Chris Lattnerd85340f2006-07-12 18:29:36 +0000144 CallSite CS = CallSite::get(Inst);
145 if (isOnlyADirectCall(F, CS))
Chris Lattnerc54b1c12006-01-14 20:03:00 +0000146 getOrInsertFunction(Inst->getParent()->getParent())
Chris Lattnerd85340f2006-07-12 18:29:36 +0000147 ->addCalledFunction(CS, Node);
Chris Lattner03839952005-12-22 06:07:52 +0000148 else
149 isUsedExternally = true;
150 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(*I)) {
151 for (Value::use_iterator I = GV->use_begin(), E = GV->use_end();
152 I != E; ++I)
153 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
Chris Lattnerd85340f2006-07-12 18:29:36 +0000154 CallSite CS = CallSite::get(Inst);
155 if (isOnlyADirectCall(F, CS))
156 getOrInsertFunction(Inst->getParent()->getParent())
157 ->addCalledFunction(CS, Node);
158 else
159 isUsedExternally = true;
Chris Lattner03839952005-12-22 06:07:52 +0000160 } else {
161 isUsedExternally = true;
162 }
163 } else { // Can't classify the user!
164 isUsedExternally = true;
165 }
Chris Lattner07a38e72003-10-31 21:05:12 +0000166 }
Chris Lattner03839952005-12-22 06:07:52 +0000167 if (isUsedExternally)
Chris Lattnerd85340f2006-07-12 18:29:36 +0000168 ExternalCallingNode->addCalledFunction(CallSite(), Node);
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000169
Chris Lattnerd85340f2006-07-12 18:29:36 +0000170 // Look for an indirect function call.
Chris Lattner03839952005-12-22 06:07:52 +0000171 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
172 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
173 II != IE; ++II) {
Chris Lattner07a38e72003-10-31 21:05:12 +0000174 CallSite CS = CallSite::get(II);
175 if (CS.getInstruction() && !CS.getCalledFunction())
Chris Lattnerd85340f2006-07-12 18:29:36 +0000176 Node->addCalledFunction(CS, CallsExternalNode);
Chris Lattner03839952005-12-22 06:07:52 +0000177 }
178 }
179
180 //
181 // destroy - Release memory for the call graph
182 virtual void destroy() {
Chris Lattnerf2d9ceb2006-12-05 19:46:12 +0000183 /// CallsExternalNode is not in the function map, delete it explicitly.
Chris Lattner12d38bf2006-12-04 21:22:45 +0000184 delete CallsExternalNode;
185 CallsExternalNode = 0;
Chris Lattnerf2d9ceb2006-12-05 19:46:12 +0000186 CallGraph::destroy();
Chris Lattner03839952005-12-22 06:07:52 +0000187 }
188};
Chris Lattner41fbf302001-09-28 00:08:15 +0000189
Chris Lattner03839952005-12-22 06:07:52 +0000190RegisterAnalysisGroup<CallGraph> X("Call Graph");
Chris Lattner7f8897f2006-08-27 22:42:52 +0000191RegisterPass<BasicCallGraph> Y("basiccg", "Basic CallGraph Construction");
Chris Lattnera5370172006-08-28 00:42:29 +0000192RegisterAnalysisGroup<CallGraph, true> Z(Y);
Chris Lattner03839952005-12-22 06:07:52 +0000193
194} //End anonymous namespace
195
196void CallGraph::initialize(Module &M) {
Chris Lattner7e708292002-06-25 16:13:24 +0000197 Mod = &M;
Chris Lattner41fbf302001-09-28 00:08:15 +0000198}
199
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000200void CallGraph::destroy() {
Chris Lattner45d10472006-10-09 17:28:13 +0000201 if (!FunctionMap.empty()) {
Chris Lattner03839952005-12-22 06:07:52 +0000202 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
203 I != E; ++I)
204 delete I->second;
205 FunctionMap.clear();
206 }
Chris Lattner25e9cad2001-11-26 18:51:25 +0000207}
208
Chris Lattneraf8a4242004-08-08 03:27:49 +0000209void CallGraph::print(std::ostream &OS, const Module *M) const {
Chris Lattner16500152002-11-04 00:21:19 +0000210 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattneraf8a4242004-08-08 03:27:49 +0000211 I->second->print(OS);
212}
213
Chris Lattner1694ec62006-01-14 19:17:02 +0000214void CallGraph::dump() const {
Bill Wendling68fe61d2006-11-29 00:19:40 +0000215 print(llvm_cerr, 0);
Chris Lattner1694ec62006-01-14 19:17:02 +0000216}
217
Chris Lattner25e9cad2001-11-26 18:51:25 +0000218//===----------------------------------------------------------------------===//
219// Implementations of public modification methods
220//
221
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000222// removeFunctionFromModule - Unlink the function from this module, returning
223// it. Because this removes the function from the module, the call graph node
224// is destroyed. This is only valid if the function does not call any other
225// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner25e9cad2001-11-26 18:51:25 +0000226// is to dropAllReferences before calling this.
227//
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000228Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
229 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
230 "graph if it references other functions!");
Chris Lattner5714c972003-08-31 20:36:52 +0000231 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000232 delete CGN; // Delete the call graph node for this func
Chris Lattner5714c972003-08-31 20:36:52 +0000233 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner25e9cad2001-11-26 18:51:25 +0000234
Chris Lattner5714c972003-08-31 20:36:52 +0000235 Mod->getFunctionList().remove(F);
236 return F;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000237}
238
Chris Lattner6f7e5eb2004-09-18 00:22:13 +0000239// changeFunction - This method changes the function associated with this
240// CallGraphNode, for use by transformations that need to change the prototype
241// of a Function (thus they must create a new Function and move the old code
242// over).
243void CallGraph::changeFunction(Function *OldF, Function *NewF) {
244 iterator I = FunctionMap.find(OldF);
245 CallGraphNode *&New = FunctionMap[NewF];
246 assert(I != FunctionMap.end() && I->second && !New &&
247 "OldF didn't exist in CG or NewF already does!");
248 New = I->second;
Chris Lattner3795bc92004-09-18 00:27:20 +0000249 New->F = NewF;
Chris Lattner6f7e5eb2004-09-18 00:22:13 +0000250 FunctionMap.erase(I);
251}
252
Chris Lattnerc54b1c12006-01-14 20:03:00 +0000253// getOrInsertFunction - This method is identical to calling operator[], but
254// it will insert a new CallGraphNode for the specified function if one does
255// not already exist.
256CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
257 CallGraphNode *&CGN = FunctionMap[F];
258 if (CGN) return CGN;
259
260 assert((!F || F->getParent() == Mod) && "Function not in current module!");
261 return CGN = new CallGraphNode(const_cast<Function*>(F));
262}
263
Chris Lattner03839952005-12-22 06:07:52 +0000264void CallGraphNode::print(std::ostream &OS) const {
265 if (Function *F = getFunction())
266 OS << "Call graph node for function: '" << F->getName() <<"'\n";
267 else
268 OS << "Call graph node <<null function: 0x" << this << ">>:\n";
269
270 for (const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerd85340f2006-07-12 18:29:36 +0000271 if (I->second->getFunction())
272 OS << " Calls function '" << I->second->getFunction()->getName() <<"'\n";
Chris Lattner03839952005-12-22 06:07:52 +0000273 else
274 OS << " Calls external node\n";
275 OS << "\n";
276}
277
Bill Wendling68fe61d2006-11-29 00:19:40 +0000278void CallGraphNode::dump() const { print(llvm_cerr); }
Chris Lattner03839952005-12-22 06:07:52 +0000279
Chris Lattnerb81c0212004-04-12 05:36:32 +0000280void CallGraphNode::removeCallEdgeTo(CallGraphNode *Callee) {
281 for (unsigned i = CalledFunctions.size(); ; --i) {
282 assert(i && "Cannot find callee to remove!");
Chris Lattnerd85340f2006-07-12 18:29:36 +0000283 if (CalledFunctions[i-1].second == Callee) {
Chris Lattnerb81c0212004-04-12 05:36:32 +0000284 CalledFunctions.erase(CalledFunctions.begin()+i-1);
285 return;
286 }
287 }
288}
Chris Lattnercd382a32004-09-18 21:34:34 +0000289
290// removeAnyCallEdgeTo - This method removes any call edges from this node to
291// the specified callee function. This takes more time to execute than
292// removeCallEdgeTo, so it should not be used unless necessary.
293void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
Chris Lattnerfff03c92004-09-19 19:01:06 +0000294 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
Chris Lattnerd85340f2006-07-12 18:29:36 +0000295 if (CalledFunctions[i].second == Callee) {
Chris Lattnerfff03c92004-09-19 19:01:06 +0000296 CalledFunctions[i] = CalledFunctions.back();
297 CalledFunctions.pop_back();
298 --i; --e;
Chris Lattnercd382a32004-09-18 21:34:34 +0000299 }
300}
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000301
302// Enuse that users of CallGraph.h also link with this file
303DEFINING_FILE_FOR(CallGraph)