blob: 9c22b7cc5e260ab4b73364a4ce3953807be6326f [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//
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 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"
Bill Wendlingafd54eb2006-11-29 00:19:40 +000019#include "llvm/Support/Streams.h"
20#include <ostream>
Chris Lattner8b6db182004-04-12 05:36:32 +000021using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000022
Chris Lattner9c4a58b2003-10-31 21:05:12 +000023static bool isOnlyADirectCall(Function *F, CallSite CS) {
24 if (!CS.getInstruction()) return false;
25 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; ++I)
26 if (*I == F) return false;
27 return true;
28}
29
Chris Lattnerbe1987772005-12-22 06:07:52 +000030namespace {
31
32//===----------------------------------------------------------------------===//
33// BasicCallGraph class definition
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000034//
Chris Lattnerbe1987772005-12-22 06:07:52 +000035class BasicCallGraph : public CallGraph, public ModulePass {
36 // Root is root of the call graph, or the external node if a 'main' function
37 // couldn't be found.
38 //
39 CallGraphNode *Root;
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000040
Chris Lattnerbe1987772005-12-22 06:07:52 +000041 // ExternalCallingNode - This node has edges to all external functions and
42 // those internal functions that have their address taken.
43 CallGraphNode *ExternalCallingNode;
Chris Lattner03946cd2001-11-26 18:51:25 +000044
Chris Lattnerbe1987772005-12-22 06:07:52 +000045 // CallsExternalNode - This node has edges to it from all functions making
46 // indirect calls or calling an external function.
47 CallGraphNode *CallsExternalNode;
48
49public:
50 BasicCallGraph() : Root(0), ExternalCallingNode(0), CallsExternalNode(0) {}
51 ~BasicCallGraph() { destroy(); }
52
53 // runOnModule - Compute the call graph for the specified module.
54 virtual bool runOnModule(Module &M) {
55 destroy();
56 CallGraph::initialize(M);
57
Chris Lattner00ca8d22006-01-14 20:03:00 +000058 ExternalCallingNode = getOrInsertFunction(0);
Chris Lattnerbe1987772005-12-22 06:07:52 +000059 CallsExternalNode = new CallGraphNode(0);
60 Root = 0;
61
62 // Add every function to the call graph...
63 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
64 addToCallGraph(I);
65
66 // If we didn't find a main function, use the external call graph node
67 if (Root == 0) Root = ExternalCallingNode;
68
69 return false;
Chris Lattnerbeed7422002-03-06 20:19:35 +000070 }
Misha Brukman01808ca2005-04-21 21:13:18 +000071
Chris Lattnerbe1987772005-12-22 06:07:52 +000072 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
73 AU.setPreservesAll();
74 }
Chris Lattnerbeed7422002-03-06 20:19:35 +000075
Bill Wendlingafd54eb2006-11-29 00:19:40 +000076 void print(llvm_ostream &o, const Module *M) const {
77 if (o.stream()) print(*o.stream(), M);
78 }
79
Chris Lattnerbe1987772005-12-22 06:07:52 +000080 virtual void print(std::ostream &o, const Module *M) const {
81 o << "CallGraph Root is: ";
82 if (Function *F = getRoot()->getFunction())
83 o << F->getName() << "\n";
84 else
85 o << "<<null function: 0x" << getRoot() << ">>\n";
86
87 CallGraph::print(o, M);
88 }
89
90 virtual void releaseMemory() {
91 destroy();
92 }
93
94 /// dump - Print out this call graph.
95 ///
96 inline void dump() const {
Bill Wendlingafd54eb2006-11-29 00:19:40 +000097 print(llvm_cerr, Mod);
Chris Lattnerbe1987772005-12-22 06:07:52 +000098 }
99
100 CallGraphNode* getExternalCallingNode() const { return ExternalCallingNode; }
101 CallGraphNode* getCallsExternalNode() const { return CallsExternalNode; }
102
103 // getRoot - Return the root of the call graph, which is either main, or if
104 // main cannot be found, the external node.
Chris Lattnerbeed7422002-03-06 20:19:35 +0000105 //
Chris Lattnerbe1987772005-12-22 06:07:52 +0000106 CallGraphNode *getRoot() { return Root; }
107 const CallGraphNode *getRoot() const { return Root; }
108
109private:
110 //===---------------------------------------------------------------------
111 // Implementation of CallGraph construction
112 //
Chris Lattnerbe1987772005-12-22 06:07:52 +0000113
Chris Lattnerbe1987772005-12-22 06:07:52 +0000114 // addToCallGraph - Add a function to the call graph, and link the node to all
115 // of the functions that it calls.
116 //
117 void addToCallGraph(Function *F) {
Chris Lattner00ca8d22006-01-14 20:03:00 +0000118 CallGraphNode *Node = getOrInsertFunction(F);
Chris Lattnerbe1987772005-12-22 06:07:52 +0000119
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000120 // If this function has external linkage, anything could call it.
Chris Lattnerbe1987772005-12-22 06:07:52 +0000121 if (!F->hasInternalLinkage()) {
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000122 ExternalCallingNode->addCalledFunction(CallSite(), Node);
Chris Lattnerbe1987772005-12-22 06:07:52 +0000123
124 // Found the entry point?
125 if (F->getName() == "main") {
126 if (Root) // Found multiple external mains? Don't pick one.
127 Root = ExternalCallingNode;
128 else
129 Root = Node; // Found a main, keep track of it!
130 }
131 }
132
133 // If this function is not defined in this translation unit, it could call
134 // anything.
135 if (F->isExternal() && !F->getIntrinsicID())
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000136 Node->addCalledFunction(CallSite(), CallsExternalNode);
Chris Lattnerbe1987772005-12-22 06:07:52 +0000137
138 // Loop over all of the users of the function... looking for callers...
139 //
140 bool isUsedExternally = false;
141 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I){
142 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000143 CallSite CS = CallSite::get(Inst);
144 if (isOnlyADirectCall(F, CS))
Chris Lattner00ca8d22006-01-14 20:03:00 +0000145 getOrInsertFunction(Inst->getParent()->getParent())
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000146 ->addCalledFunction(CS, Node);
Chris Lattnerbe1987772005-12-22 06:07:52 +0000147 else
148 isUsedExternally = true;
149 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(*I)) {
150 for (Value::use_iterator I = GV->use_begin(), E = GV->use_end();
151 I != E; ++I)
152 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000153 CallSite CS = CallSite::get(Inst);
154 if (isOnlyADirectCall(F, CS))
155 getOrInsertFunction(Inst->getParent()->getParent())
156 ->addCalledFunction(CS, Node);
157 else
158 isUsedExternally = true;
Chris Lattnerbe1987772005-12-22 06:07:52 +0000159 } else {
160 isUsedExternally = true;
161 }
162 } else { // Can't classify the user!
163 isUsedExternally = true;
164 }
Chris Lattner9c4a58b2003-10-31 21:05:12 +0000165 }
Chris Lattnerbe1987772005-12-22 06:07:52 +0000166 if (isUsedExternally)
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000167 ExternalCallingNode->addCalledFunction(CallSite(), Node);
Chris Lattnerbeed7422002-03-06 20:19:35 +0000168
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000169 // Look for an indirect function call.
Chris Lattnerbe1987772005-12-22 06:07:52 +0000170 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
171 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
172 II != IE; ++II) {
Chris Lattner9c4a58b2003-10-31 21:05:12 +0000173 CallSite CS = CallSite::get(II);
174 if (CS.getInstruction() && !CS.getCalledFunction())
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000175 Node->addCalledFunction(CS, CallsExternalNode);
Chris Lattnerbe1987772005-12-22 06:07:52 +0000176 }
177 }
178
179 //
180 // destroy - Release memory for the call graph
181 virtual void destroy() {
182 if (!CallsExternalNode) {
183 delete CallsExternalNode;
184 CallsExternalNode = 0;
Chris Lattnerbeed7422002-03-06 20:19:35 +0000185 }
Chris Lattnerbe1987772005-12-22 06:07:52 +0000186 }
187};
Chris Lattnerbbf3ae82001-09-28 00:08:15 +0000188
Chris Lattnerbe1987772005-12-22 06:07:52 +0000189RegisterAnalysisGroup<CallGraph> X("Call Graph");
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000190RegisterPass<BasicCallGraph> Y("basiccg", "Basic CallGraph Construction");
Chris Lattner97c9f202006-08-28 00:42:29 +0000191RegisterAnalysisGroup<CallGraph, true> Z(Y);
Chris Lattnerbe1987772005-12-22 06:07:52 +0000192
193} //End anonymous namespace
194
195void CallGraph::initialize(Module &M) {
Chris Lattnerccf571a2002-01-31 00:42:27 +0000196 destroy();
Chris Lattner113f4f42002-06-25 16:13:24 +0000197 Mod = &M;
Chris Lattnerbbf3ae82001-09-28 00:08:15 +0000198}
199
Chris Lattner80327322002-03-06 17:16:43 +0000200void CallGraph::destroy() {
Chris Lattneraba5e1e2006-10-09 17:28:13 +0000201 if (!FunctionMap.empty()) {
Chris Lattnerbe1987772005-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 Lattner03946cd2001-11-26 18:51:25 +0000207}
208
Chris Lattner6b5411022004-08-08 03:27:49 +0000209void CallGraph::print(std::ostream &OS, const Module *M) const {
Chris Lattnerb9d55472002-11-04 00:21:19 +0000210 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner6b5411022004-08-08 03:27:49 +0000211 I->second->print(OS);
212}
213
Chris Lattnerbc351e12006-01-14 19:17:02 +0000214void CallGraph::dump() const {
Bill Wendlingafd54eb2006-11-29 00:19:40 +0000215 print(llvm_cerr, 0);
Chris Lattnerbc351e12006-01-14 19:17:02 +0000216}
217
Chris Lattner03946cd2001-11-26 18:51:25 +0000218//===----------------------------------------------------------------------===//
219// Implementations of public modification methods
220//
221
Chris Lattner79b0c7d2002-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 Lattner03946cd2001-11-26 18:51:25 +0000226// is to dropAllReferences before calling this.
227//
Chris Lattner79b0c7d2002-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 Lattner6f5e18d2003-08-31 20:36:52 +0000231 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000232 delete CGN; // Delete the call graph node for this func
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000233 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner03946cd2001-11-26 18:51:25 +0000234
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000235 Mod->getFunctionList().remove(F);
236 return F;
Chris Lattner03946cd2001-11-26 18:51:25 +0000237}
238
Chris Lattnere81c2aa2004-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 Lattner85d5ccc2004-09-18 00:27:20 +0000249 New->F = NewF;
Chris Lattnere81c2aa2004-09-18 00:22:13 +0000250 FunctionMap.erase(I);
251}
252
Chris Lattner00ca8d22006-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 Lattnerbe1987772005-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 Lattner5de3b8b2006-07-12 18:29:36 +0000271 if (I->second->getFunction())
272 OS << " Calls function '" << I->second->getFunction()->getName() <<"'\n";
Chris Lattnerbe1987772005-12-22 06:07:52 +0000273 else
274 OS << " Calls external node\n";
275 OS << "\n";
276}
277
Bill Wendlingafd54eb2006-11-29 00:19:40 +0000278void CallGraphNode::dump() const { print(llvm_cerr); }
Chris Lattnerbe1987772005-12-22 06:07:52 +0000279
Chris Lattner8b6db182004-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 Lattner5de3b8b2006-07-12 18:29:36 +0000283 if (CalledFunctions[i-1].second == Callee) {
Chris Lattner8b6db182004-04-12 05:36:32 +0000284 CalledFunctions.erase(CalledFunctions.begin()+i-1);
285 return;
286 }
287 }
288}
Chris Lattner824a2182004-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 Lattnerd6d99df2004-09-19 19:01:06 +0000294 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000295 if (CalledFunctions[i].second == Callee) {
Chris Lattnerd6d99df2004-09-19 19:01:06 +0000296 CalledFunctions[i] = CalledFunctions.back();
297 CalledFunctions.pop_back();
298 --i; --e;
Chris Lattner824a2182004-09-18 21:34:34 +0000299 }
300}
Reid Spencerbe535662006-06-07 22:00:26 +0000301
302// Enuse that users of CallGraph.h also link with this file
303DEFINING_FILE_FOR(CallGraph)