blob: 506198c4fa2682a07fd31a33db57d7714060a59c [file] [log] [blame]
Chris Lattnerd852cc32002-03-06 18:00:49 +00001//===- CallGraph.cpp - Build a Module's call graph ------------------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerbbf3ae82001-09-28 00:08:15 +00009//
Chris Lattnerbeed7422002-03-06 20:19:35 +000010// This interface is used to build and manipulate a call graph, which is a very
11// useful tool for interprocedural optimization.
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000012//
Chris Lattner79b0c7d2002-07-18 04:43:16 +000013// Every function in a module is represented as a node in the call graph. The
14// callgraph node keeps track of which functions the are called by the function
Chris Lattnerbeed7422002-03-06 20:19:35 +000015// corresponding to the node.
16//
Chris Lattner79b0c7d2002-07-18 04:43:16 +000017// A call graph will contain nodes where the function that they correspond to is
Chris Lattnerbeed7422002-03-06 20:19:35 +000018// null. This 'external' node is used to represent control flow that is not
19// represented (or analyzable) in the module. As such, the external node will
Chris Lattner79b0c7d2002-07-18 04:43:16 +000020// have edges to functions with the following properties:
21// 1. All functions in the module without internal linkage, since they could
22// be called by functions outside of the our analysis capability.
23// 2. All functions whose address is used for something more than a direct
24// call, for example being stored into a memory location. Since they may
25// be called by an unknown caller later, they must be tracked as such.
Chris Lattnerbeed7422002-03-06 20:19:35 +000026//
Chris Lattner79b0c7d2002-07-18 04:43:16 +000027// Similarly, functions have a call edge to the external node iff:
28// 1. The function is external, reflecting the fact that they could call
Chris Lattnerbeed7422002-03-06 20:19:35 +000029// anything without internal linkage or that has its address taken.
Chris Lattner79b0c7d2002-07-18 04:43:16 +000030// 2. The function contains an indirect function call.
Chris Lattnerbeed7422002-03-06 20:19:35 +000031//
32// As an extension in the future, there may be multiple nodes with a null
Chris Lattner79b0c7d2002-07-18 04:43:16 +000033// function. These will be used when we can prove (through pointer analysis)
34// that an indirect call site can call only a specific set of functions.
Chris Lattnerbeed7422002-03-06 20:19:35 +000035//
36// Because of these properties, the CallGraph captures a conservative superset
37// of all of the caller-callee relationships, which is useful for
38// transformations.
39//
40// The CallGraph class also attempts to figure out what the root of the
Chris Lattner79b0c7d2002-07-18 04:43:16 +000041// CallGraph is, which is currently does by looking for a function named 'main'.
42// If no function named 'main' is found, the external node is used as the entry
43// node, reflecting the fact that any function without internal linkage could
Chris Lattnerbeed7422002-03-06 20:19:35 +000044// be called into (which is common for libraries).
Chris Lattner8cbbbef2001-10-13 06:33:19 +000045//
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000046//===----------------------------------------------------------------------===//
47
48#include "llvm/Analysis/CallGraph.h"
Chris Lattner9c4a58b2003-10-31 21:05:12 +000049#include "llvm/Constants.h" // Remove when ConstantPointerRefs are gone
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000050#include "llvm/Module.h"
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000051#include "llvm/iOther.h"
Chris Lattner8cbbbef2001-10-13 06:33:19 +000052#include "llvm/iTerminators.h"
Chris Lattner9c4a58b2003-10-31 21:05:12 +000053#include "llvm/Support/CallSite.h"
Chris Lattner5de22042001-11-27 00:03:19 +000054#include "Support/STLExtras.h"
Chris Lattnera2c09852002-07-26 21:12:44 +000055
Brian Gaeke960707c2003-11-11 22:41:34 +000056namespace llvm {
57
Chris Lattnera2c09852002-07-26 21:12:44 +000058static RegisterAnalysis<CallGraph> X("callgraph", "Call Graph Construction");
Chris Lattnerccf571a2002-01-31 00:42:27 +000059
Chris Lattner5987ab82003-10-22 18:53:31 +000060static const char * const KnownExternalFunctions[] = {
61 // Low-level system calls
62 "open",
63 "read",
64 "write",
65 "writev",
66 "lseek",
67 "poll",
68 "ioctl",
69
70 // Low-level stdc library functions
Chris Lattner9a010032003-11-09 19:54:30 +000071 "abort", "exit",
72 "getenv", "putenv",
Chris Lattner5987ab82003-10-22 18:53:31 +000073
74 // Standard IO functions
75 "printf",
76 "sprintf",
77 "fopen",
78 "freopen",
79 "fclose",
80 "fwrite",
81 "puts",
82 "fputs",
83 "getc",
84 "ungetc",
85 "putc",
86 "putchar",
87 "fread",
88 "fileno",
89 "ftell",
90 "fflush",
91 "fseek",
92 "fileno",
93 "ferror",
94 "feof",
95 "fdopen",
96 "__fxstat",
97 "setbuf",
98 "setbuffer",
99 "etlinebuf",
100 "setvbuf",
101
102 // Memory functions
103 "malloc",
104 "free",
105 "realloc",
106 "calloc",
107 "memalign",
108
109 // String functions
110 "atoi",
111 "memmove",
112 "memset",
113 "memchr",
114 "memcmp",
115 "strchr",
116 "strncpy",
117 "strncmp",
118 "strcmp",
Chris Lattner9a010032003-11-09 19:54:30 +0000119 "strtok",
Chris Lattner5987ab82003-10-22 18:53:31 +0000120 "__strcoll_l",
121 "__strxfrm_l",
122 "__strftime_l",
123 "__strtol_l",
124 "__strtoul_l",
125 "__strtoll_l",
126 "__strtoull_l",
127 "__strtof_l",
128 "__strtod_l",
129 "__strtold_l",
Chris Lattner9a010032003-11-09 19:54:30 +0000130 "isalpha",
Chris Lattner5987ab82003-10-22 18:53:31 +0000131
Chris Lattner950c75f2003-11-09 04:10:41 +0000132 // Math functions
133 "exp", "sqrt", "cbrt", "hypot",
134 "log", "log10", "pow",
135 "sin", "cos", "tan",
136 "asin", "acos", "atan", "atan2",
137
Chris Lattner5987ab82003-10-22 18:53:31 +0000138 // Locale functions
139 "__uselocale",
140 "__newlocale",
141 "__freelocale",
142 "__duplocale",
143 "__nl_langinfo_l",
144
145 // gettext functions used by libstdc++
146 "gettext",
147 "dgettext",
148 "dcgettext",
149 "textdomain",
150 "bindtextdomain",
151
152 // Random stuff
153 "__assert_fail",
154 "__errno_location",
Chris Lattner950c75f2003-11-09 04:10:41 +0000155 "clock", "time",
156 "__main",
Chris Lattner5987ab82003-10-22 18:53:31 +0000157};
158
159
160/// ExternalFunctionDoesntCallIntoProgram - This hack is used to indicate to the
161/// call graph that the specified external function is _KNOWN_ to not call back
162/// into the program. This is important, because otherwise functions which call
163/// "printf" for example, end up in a great big SCC that goes from the function
164/// through main.
165///
166static bool ExternalFunctionDoesntCallIntoProgram(const std::string &Name) {
167 static std::vector<std::string> Funcs;
168
169 // First time this is called?
170 if (Funcs.empty()) {
171 // Add a whole bunch of functions which are often used...
172 Funcs.insert(Funcs.end(), KnownExternalFunctions,
173 KnownExternalFunctions+
174 sizeof(KnownExternalFunctions)/sizeof(KnownExternalFunctions[0]));
175 // Sort the list for efficient access
176 std::sort(Funcs.begin(), Funcs.end());
177 }
178
Chris Lattner5011b952003-11-09 04:00:59 +0000179 if (Name.size() > 7 && !memcmp("__llvm_", Name.c_str(), 7))
180 return true;
181
Chris Lattner5987ab82003-10-22 18:53:31 +0000182 // Binary search for the function name...
183 std::vector<std::string>::iterator I =
184 std::lower_bound(Funcs.begin(), Funcs.end(), Name);
185
186 // Found it?
187 return I != Funcs.end() && *I == Name;
188}
189
190
191
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000192// getNodeFor - Return the node for the specified function or create one if it
Chris Lattnerbbf3ae82001-09-28 00:08:15 +0000193// does not already exist.
194//
Chris Lattner0df67e32002-03-26 17:55:33 +0000195CallGraphNode *CallGraph::getNodeFor(Function *F) {
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000196 CallGraphNode *&CGN = FunctionMap[F];
Chris Lattnerbeed7422002-03-06 20:19:35 +0000197 if (CGN) return CGN;
Chris Lattnerbbf3ae82001-09-28 00:08:15 +0000198
Chris Lattner0df67e32002-03-26 17:55:33 +0000199 assert((!F || F->getParent() == Mod) && "Function not in current module!");
200 return CGN = new CallGraphNode(F);
Chris Lattnerbbf3ae82001-09-28 00:08:15 +0000201}
202
Chris Lattner9c4a58b2003-10-31 21:05:12 +0000203static bool isOnlyADirectCall(Function *F, CallSite CS) {
204 if (!CS.getInstruction()) return false;
205 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; ++I)
206 if (*I == F) return false;
207 return true;
208}
209
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000210// addToCallGraph - Add a function to the call graph, and link the node to all
211// of the functions that it calls.
Chris Lattnerbbf3ae82001-09-28 00:08:15 +0000212//
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000213void CallGraph::addToCallGraph(Function *F) {
214 CallGraphNode *Node = getNodeFor(F);
Chris Lattnerbbf3ae82001-09-28 00:08:15 +0000215
Chris Lattner233f2a02003-09-15 04:35:16 +0000216 // If this function has external linkage, anything could call it...
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000217 if (!F->hasInternalLinkage()) {
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000218 ExternalNode->addCalledFunction(Node);
Chris Lattner03946cd2001-11-26 18:51:25 +0000219
Chris Lattnerbeed7422002-03-06 20:19:35 +0000220 // Found the entry point?
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000221 if (F->getName() == "main") {
Chris Lattnerbeed7422002-03-06 20:19:35 +0000222 if (Root)
223 Root = ExternalNode; // Found multiple external mains? Don't pick one.
224 else
225 Root = Node; // Found a main, keep track of it!
226 }
Chris Lattnerbeed7422002-03-06 20:19:35 +0000227 }
Chris Lattner233f2a02003-09-15 04:35:16 +0000228
229 // If this function is not defined in this translation unit, it could call
230 // anything.
Chris Lattner5987ab82003-10-22 18:53:31 +0000231 if (F->isExternal() && !F->getIntrinsicID() &&
232 !ExternalFunctionDoesntCallIntoProgram(F->getName()))
Chris Lattner233f2a02003-09-15 04:35:16 +0000233 Node->addCalledFunction(ExternalNode);
Chris Lattnerbeed7422002-03-06 20:19:35 +0000234
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000235 // Loop over all of the users of the function... looking for callers...
Chris Lattnerbeed7422002-03-06 20:19:35 +0000236 //
Chris Lattner9c4a58b2003-10-31 21:05:12 +0000237 bool isUsedExternally = false;
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000238 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I) {
Chris Lattner9c4a58b2003-10-31 21:05:12 +0000239 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
240 if (isOnlyADirectCall(F, CallSite::get(Inst)))
241 getNodeFor(Inst->getParent()->getParent())->addCalledFunction(Node);
242 else
243 isUsedExternally = true;
244 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(*I)) {
245 // THIS IS A DISGUSTING HACK. Brought to you by the power of
246 // ConstantPointerRefs!
247 for (Value::use_iterator I = CPR->use_begin(), E = CPR->use_end();
248 I != E; ++I)
249 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
250 if (isOnlyADirectCall(F, CallSite::get(Inst)))
251 getNodeFor(Inst->getParent()->getParent())->addCalledFunction(Node);
252 else
253 isUsedExternally = true;
254 } else {
255 isUsedExternally = true;
256 }
257 } else { // Can't classify the user!
258 isUsedExternally = true;
259 }
Chris Lattnerbeed7422002-03-06 20:19:35 +0000260 }
Chris Lattner9c4a58b2003-10-31 21:05:12 +0000261 if (isUsedExternally)
262 ExternalNode->addCalledFunction(Node);
Chris Lattnerbeed7422002-03-06 20:19:35 +0000263
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000264 // Look for an indirect function call...
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000265 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
Chris Lattnerbeed7422002-03-06 20:19:35 +0000266 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II){
Chris Lattner9c4a58b2003-10-31 21:05:12 +0000267 CallSite CS = CallSite::get(II);
268 if (CS.getInstruction() && !CS.getCalledFunction())
269 Node->addCalledFunction(ExternalNode);
Chris Lattnerbeed7422002-03-06 20:19:35 +0000270 }
Chris Lattnerbbf3ae82001-09-28 00:08:15 +0000271}
272
Chris Lattner113f4f42002-06-25 16:13:24 +0000273bool CallGraph::run(Module &M) {
Chris Lattnerccf571a2002-01-31 00:42:27 +0000274 destroy();
275
Chris Lattner113f4f42002-06-25 16:13:24 +0000276 Mod = &M;
Chris Lattnerbeed7422002-03-06 20:19:35 +0000277 ExternalNode = getNodeFor(0);
278 Root = 0;
Chris Lattner03946cd2001-11-26 18:51:25 +0000279
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000280 // Add every function to the call graph...
Chris Lattner113f4f42002-06-25 16:13:24 +0000281 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
282 addToCallGraph(I);
Chris Lattnerbeed7422002-03-06 20:19:35 +0000283
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000284 // If we didn't find a main function, use the external call graph node
Chris Lattnerbeed7422002-03-06 20:19:35 +0000285 if (Root == 0) Root = ExternalNode;
Chris Lattnerccf571a2002-01-31 00:42:27 +0000286
287 return false;
Chris Lattnerbbf3ae82001-09-28 00:08:15 +0000288}
289
Chris Lattner80327322002-03-06 17:16:43 +0000290void CallGraph::destroy() {
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000291 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
Chris Lattnerbeed7422002-03-06 20:19:35 +0000292 I != E; ++I)
Chris Lattner03946cd2001-11-26 18:51:25 +0000293 delete I->second;
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000294 FunctionMap.clear();
Chris Lattner03946cd2001-11-26 18:51:25 +0000295}
296
Chris Lattnerb9d55472002-11-04 00:21:19 +0000297static void WriteToOutput(const CallGraphNode *CGN, std::ostream &o) {
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000298 if (CGN->getFunction())
299 o << "Call graph node for function: '"
300 << CGN->getFunction()->getName() <<"'\n";
Chris Lattner03946cd2001-11-26 18:51:25 +0000301 else
Chris Lattner5d0d3a22003-09-15 04:29:37 +0000302 o << "Call graph node <<null function: 0x" << CGN << ">>:\n";
Chris Lattner03946cd2001-11-26 18:51:25 +0000303
Chris Lattnerbbf3ae82001-09-28 00:08:15 +0000304 for (unsigned i = 0; i < CGN->size(); ++i)
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000305 if ((*CGN)[i]->getFunction())
306 o << " Calls function '" << (*CGN)[i]->getFunction()->getName() << "'\n";
Chris Lattnerbeed7422002-03-06 20:19:35 +0000307 else
308 o << " Calls external node\n";
Chris Lattner7f74a562002-01-20 22:54:45 +0000309 o << "\n";
Chris Lattnerbbf3ae82001-09-28 00:08:15 +0000310}
311
Chris Lattnerb9d55472002-11-04 00:21:19 +0000312void CallGraph::print(std::ostream &o, const Module *M) const {
Chris Lattner5d0d3a22003-09-15 04:29:37 +0000313 o << "CallGraph Root is: ";
314 if (getRoot()->getFunction())
315 o << getRoot()->getFunction()->getName() << "\n";
316 else
317 o << "<<null function: 0x" << getRoot() << ">>\n";
318
Chris Lattnerb9d55472002-11-04 00:21:19 +0000319 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
320 WriteToOutput(I->second, o);
Chris Lattnerbbf3ae82001-09-28 00:08:15 +0000321}
Vikram S. Adve5dab57d2001-10-22 13:55:46 +0000322
323
Chris Lattner03946cd2001-11-26 18:51:25 +0000324//===----------------------------------------------------------------------===//
325// Implementations of public modification methods
326//
327
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000328// Functions to keep a call graph up to date with a function that has been
Chris Lattner03946cd2001-11-26 18:51:25 +0000329// modified
330//
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000331void CallGraph::addFunctionToModule(Function *Meth) {
Chris Lattner03946cd2001-11-26 18:51:25 +0000332 assert(0 && "not implemented");
333 abort();
334}
335
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000336// removeFunctionFromModule - Unlink the function from this module, returning
337// it. Because this removes the function from the module, the call graph node
338// is destroyed. This is only valid if the function does not call any other
339// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner03946cd2001-11-26 18:51:25 +0000340// is to dropAllReferences before calling this.
341//
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000342Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
343 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
344 "graph if it references other functions!");
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000345 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000346 delete CGN; // Delete the call graph node for this func
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000347 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner03946cd2001-11-26 18:51:25 +0000348
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000349 Mod->getFunctionList().remove(F);
350 return F;
Chris Lattner03946cd2001-11-26 18:51:25 +0000351}
352
Chris Lattnerdd63f9e2003-10-30 05:17:30 +0000353void CallGraph::stub() {}
Brian Gaeke960707c2003-11-11 22:41:34 +0000354
355} // End llvm namespace