blob: 8d442420500af8954117f776cce9942d4e0085af [file] [log] [blame]
Chris Lattner8d5a16c2002-03-06 18:00:49 +00001//===- CallGraph.cpp - Build a Module's call graph ------------------------===//
John Criswellb576c942003-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 Lattner41fbf302001-09-28 00:08:15 +00009//
Chris Lattnerd4d427b2002-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 Lattner41fbf302001-09-28 00:08:15 +000012//
Chris Lattnerd99d4d72002-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 Lattnerd4d427b2002-03-06 20:19:35 +000015// corresponding to the node.
16//
Chris Lattnerd99d4d72002-07-18 04:43:16 +000017// A call graph will contain nodes where the function that they correspond to is
Chris Lattnerd4d427b2002-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 Lattnerd99d4d72002-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 Lattnerd4d427b2002-03-06 20:19:35 +000026//
Chris Lattnerd99d4d72002-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 Lattnerd4d427b2002-03-06 20:19:35 +000029// anything without internal linkage or that has its address taken.
Chris Lattnerd99d4d72002-07-18 04:43:16 +000030// 2. The function contains an indirect function call.
Chris Lattnerd4d427b2002-03-06 20:19:35 +000031//
32// As an extension in the future, there may be multiple nodes with a null
Chris Lattnerd99d4d72002-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 Lattnerd4d427b2002-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 Lattnerd99d4d72002-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 Lattnerd4d427b2002-03-06 20:19:35 +000044// be called into (which is common for libraries).
Chris Lattner9f9e2be2001-10-13 06:33:19 +000045//
Chris Lattner41fbf302001-09-28 00:08:15 +000046//===----------------------------------------------------------------------===//
47
48#include "llvm/Analysis/CallGraph.h"
Chris Lattner07a38e72003-10-31 21:05:12 +000049#include "llvm/Constants.h" // Remove when ConstantPointerRefs are gone
Chris Lattner41fbf302001-09-28 00:08:15 +000050#include "llvm/Module.h"
Chris Lattner41fbf302001-09-28 00:08:15 +000051#include "llvm/iOther.h"
Chris Lattner9f9e2be2001-10-13 06:33:19 +000052#include "llvm/iTerminators.h"
Chris Lattner07a38e72003-10-31 21:05:12 +000053#include "llvm/Support/CallSite.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000054#include "Support/STLExtras.h"
Chris Lattner1e435162002-07-26 21:12:44 +000055
56static RegisterAnalysis<CallGraph> X("callgraph", "Call Graph Construction");
Chris Lattner93193f82002-01-31 00:42:27 +000057
Chris Lattnerb7c4c992003-10-22 18:53:31 +000058static const char * const KnownExternalFunctions[] = {
59 // Low-level system calls
60 "open",
61 "read",
62 "write",
63 "writev",
64 "lseek",
65 "poll",
66 "ioctl",
67
68 // Low-level stdc library functions
Chris Lattnerccc4b1a2003-11-09 19:54:30 +000069 "abort", "exit",
70 "getenv", "putenv",
Chris Lattnerb7c4c992003-10-22 18:53:31 +000071
72 // Standard IO functions
73 "printf",
74 "sprintf",
75 "fopen",
76 "freopen",
77 "fclose",
78 "fwrite",
79 "puts",
80 "fputs",
81 "getc",
82 "ungetc",
83 "putc",
84 "putchar",
85 "fread",
86 "fileno",
87 "ftell",
88 "fflush",
89 "fseek",
90 "fileno",
91 "ferror",
92 "feof",
93 "fdopen",
94 "__fxstat",
95 "setbuf",
96 "setbuffer",
97 "etlinebuf",
98 "setvbuf",
99
100 // Memory functions
101 "malloc",
102 "free",
103 "realloc",
104 "calloc",
105 "memalign",
106
107 // String functions
108 "atoi",
109 "memmove",
110 "memset",
111 "memchr",
112 "memcmp",
113 "strchr",
114 "strncpy",
115 "strncmp",
116 "strcmp",
Chris Lattnerccc4b1a2003-11-09 19:54:30 +0000117 "strtok",
Chris Lattnerb7c4c992003-10-22 18:53:31 +0000118 "__strcoll_l",
119 "__strxfrm_l",
120 "__strftime_l",
121 "__strtol_l",
122 "__strtoul_l",
123 "__strtoll_l",
124 "__strtoull_l",
125 "__strtof_l",
126 "__strtod_l",
127 "__strtold_l",
Chris Lattnerccc4b1a2003-11-09 19:54:30 +0000128 "isalpha",
Chris Lattnerb7c4c992003-10-22 18:53:31 +0000129
Chris Lattnercda23472003-11-09 04:10:41 +0000130 // Math functions
131 "exp", "sqrt", "cbrt", "hypot",
132 "log", "log10", "pow",
133 "sin", "cos", "tan",
134 "asin", "acos", "atan", "atan2",
135
Chris Lattnerb7c4c992003-10-22 18:53:31 +0000136 // Locale functions
137 "__uselocale",
138 "__newlocale",
139 "__freelocale",
140 "__duplocale",
141 "__nl_langinfo_l",
142
143 // gettext functions used by libstdc++
144 "gettext",
145 "dgettext",
146 "dcgettext",
147 "textdomain",
148 "bindtextdomain",
149
150 // Random stuff
151 "__assert_fail",
152 "__errno_location",
Chris Lattnercda23472003-11-09 04:10:41 +0000153 "clock", "time",
154 "__main",
Chris Lattnerb7c4c992003-10-22 18:53:31 +0000155};
156
157
158/// ExternalFunctionDoesntCallIntoProgram - This hack is used to indicate to the
159/// call graph that the specified external function is _KNOWN_ to not call back
160/// into the program. This is important, because otherwise functions which call
161/// "printf" for example, end up in a great big SCC that goes from the function
162/// through main.
163///
164static bool ExternalFunctionDoesntCallIntoProgram(const std::string &Name) {
165 static std::vector<std::string> Funcs;
166
167 // First time this is called?
168 if (Funcs.empty()) {
169 // Add a whole bunch of functions which are often used...
170 Funcs.insert(Funcs.end(), KnownExternalFunctions,
171 KnownExternalFunctions+
172 sizeof(KnownExternalFunctions)/sizeof(KnownExternalFunctions[0]));
173 // Sort the list for efficient access
174 std::sort(Funcs.begin(), Funcs.end());
175 }
176
Chris Lattner4d728e82003-11-09 04:00:59 +0000177 if (Name.size() > 7 && !memcmp("__llvm_", Name.c_str(), 7))
178 return true;
179
Chris Lattnerb7c4c992003-10-22 18:53:31 +0000180 // Binary search for the function name...
181 std::vector<std::string>::iterator I =
182 std::lower_bound(Funcs.begin(), Funcs.end(), Name);
183
184 // Found it?
185 return I != Funcs.end() && *I == Name;
186}
187
188
189
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000190// getNodeFor - Return the node for the specified function or create one if it
Chris Lattner41fbf302001-09-28 00:08:15 +0000191// does not already exist.
192//
Chris Lattnere590ff22002-03-26 17:55:33 +0000193CallGraphNode *CallGraph::getNodeFor(Function *F) {
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000194 CallGraphNode *&CGN = FunctionMap[F];
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000195 if (CGN) return CGN;
Chris Lattner41fbf302001-09-28 00:08:15 +0000196
Chris Lattnere590ff22002-03-26 17:55:33 +0000197 assert((!F || F->getParent() == Mod) && "Function not in current module!");
198 return CGN = new CallGraphNode(F);
Chris Lattner41fbf302001-09-28 00:08:15 +0000199}
200
Chris Lattner07a38e72003-10-31 21:05:12 +0000201static bool isOnlyADirectCall(Function *F, CallSite CS) {
202 if (!CS.getInstruction()) return false;
203 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; ++I)
204 if (*I == F) return false;
205 return true;
206}
207
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000208// addToCallGraph - Add a function to the call graph, and link the node to all
209// of the functions that it calls.
Chris Lattner41fbf302001-09-28 00:08:15 +0000210//
Chris Lattner5714c972003-08-31 20:36:52 +0000211void CallGraph::addToCallGraph(Function *F) {
212 CallGraphNode *Node = getNodeFor(F);
Chris Lattner41fbf302001-09-28 00:08:15 +0000213
Chris Lattnerf52d01b2003-09-15 04:35:16 +0000214 // If this function has external linkage, anything could call it...
Chris Lattner5714c972003-08-31 20:36:52 +0000215 if (!F->hasInternalLinkage()) {
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000216 ExternalNode->addCalledFunction(Node);
Chris Lattner25e9cad2001-11-26 18:51:25 +0000217
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000218 // Found the entry point?
Chris Lattner5714c972003-08-31 20:36:52 +0000219 if (F->getName() == "main") {
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000220 if (Root)
221 Root = ExternalNode; // Found multiple external mains? Don't pick one.
222 else
223 Root = Node; // Found a main, keep track of it!
224 }
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000225 }
Chris Lattnerf52d01b2003-09-15 04:35:16 +0000226
227 // If this function is not defined in this translation unit, it could call
228 // anything.
Chris Lattnerb7c4c992003-10-22 18:53:31 +0000229 if (F->isExternal() && !F->getIntrinsicID() &&
230 !ExternalFunctionDoesntCallIntoProgram(F->getName()))
Chris Lattnerf52d01b2003-09-15 04:35:16 +0000231 Node->addCalledFunction(ExternalNode);
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000232
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000233 // Loop over all of the users of the function... looking for callers...
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000234 //
Chris Lattner07a38e72003-10-31 21:05:12 +0000235 bool isUsedExternally = false;
Chris Lattner5714c972003-08-31 20:36:52 +0000236 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I) {
Chris Lattner07a38e72003-10-31 21:05:12 +0000237 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
238 if (isOnlyADirectCall(F, CallSite::get(Inst)))
239 getNodeFor(Inst->getParent()->getParent())->addCalledFunction(Node);
240 else
241 isUsedExternally = true;
242 } else if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(*I)) {
243 // THIS IS A DISGUSTING HACK. Brought to you by the power of
244 // ConstantPointerRefs!
245 for (Value::use_iterator I = CPR->use_begin(), E = CPR->use_end();
246 I != E; ++I)
247 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
248 if (isOnlyADirectCall(F, CallSite::get(Inst)))
249 getNodeFor(Inst->getParent()->getParent())->addCalledFunction(Node);
250 else
251 isUsedExternally = true;
252 } else {
253 isUsedExternally = true;
254 }
255 } else { // Can't classify the user!
256 isUsedExternally = true;
257 }
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000258 }
Chris Lattner07a38e72003-10-31 21:05:12 +0000259 if (isUsedExternally)
260 ExternalNode->addCalledFunction(Node);
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000261
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000262 // Look for an indirect function call...
Chris Lattner5714c972003-08-31 20:36:52 +0000263 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000264 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II){
Chris Lattner07a38e72003-10-31 21:05:12 +0000265 CallSite CS = CallSite::get(II);
266 if (CS.getInstruction() && !CS.getCalledFunction())
267 Node->addCalledFunction(ExternalNode);
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000268 }
Chris Lattner41fbf302001-09-28 00:08:15 +0000269}
270
Chris Lattner7e708292002-06-25 16:13:24 +0000271bool CallGraph::run(Module &M) {
Chris Lattner93193f82002-01-31 00:42:27 +0000272 destroy();
273
Chris Lattner7e708292002-06-25 16:13:24 +0000274 Mod = &M;
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000275 ExternalNode = getNodeFor(0);
276 Root = 0;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000277
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000278 // Add every function to the call graph...
Chris Lattner7e708292002-06-25 16:13:24 +0000279 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
280 addToCallGraph(I);
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000281
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000282 // If we didn't find a main function, use the external call graph node
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000283 if (Root == 0) Root = ExternalNode;
Chris Lattner93193f82002-01-31 00:42:27 +0000284
285 return false;
Chris Lattner41fbf302001-09-28 00:08:15 +0000286}
287
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000288void CallGraph::destroy() {
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000289 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000290 I != E; ++I)
Chris Lattner25e9cad2001-11-26 18:51:25 +0000291 delete I->second;
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000292 FunctionMap.clear();
Chris Lattner25e9cad2001-11-26 18:51:25 +0000293}
294
Chris Lattner16500152002-11-04 00:21:19 +0000295static void WriteToOutput(const CallGraphNode *CGN, std::ostream &o) {
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000296 if (CGN->getFunction())
297 o << "Call graph node for function: '"
298 << CGN->getFunction()->getName() <<"'\n";
Chris Lattner25e9cad2001-11-26 18:51:25 +0000299 else
Chris Lattnerb31247a2003-09-15 04:29:37 +0000300 o << "Call graph node <<null function: 0x" << CGN << ">>:\n";
Chris Lattner25e9cad2001-11-26 18:51:25 +0000301
Chris Lattner41fbf302001-09-28 00:08:15 +0000302 for (unsigned i = 0; i < CGN->size(); ++i)
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000303 if ((*CGN)[i]->getFunction())
304 o << " Calls function '" << (*CGN)[i]->getFunction()->getName() << "'\n";
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000305 else
306 o << " Calls external node\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000307 o << "\n";
Chris Lattner41fbf302001-09-28 00:08:15 +0000308}
309
Chris Lattner16500152002-11-04 00:21:19 +0000310void CallGraph::print(std::ostream &o, const Module *M) const {
Chris Lattnerb31247a2003-09-15 04:29:37 +0000311 o << "CallGraph Root is: ";
312 if (getRoot()->getFunction())
313 o << getRoot()->getFunction()->getName() << "\n";
314 else
315 o << "<<null function: 0x" << getRoot() << ">>\n";
316
Chris Lattner16500152002-11-04 00:21:19 +0000317 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
318 WriteToOutput(I->second, o);
Chris Lattner41fbf302001-09-28 00:08:15 +0000319}
Vikram S. Advea7edb182001-10-22 13:55:46 +0000320
321
Chris Lattner25e9cad2001-11-26 18:51:25 +0000322//===----------------------------------------------------------------------===//
323// Implementations of public modification methods
324//
325
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000326// Functions to keep a call graph up to date with a function that has been
Chris Lattner25e9cad2001-11-26 18:51:25 +0000327// modified
328//
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000329void CallGraph::addFunctionToModule(Function *Meth) {
Chris Lattner25e9cad2001-11-26 18:51:25 +0000330 assert(0 && "not implemented");
331 abort();
332}
333
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000334// removeFunctionFromModule - Unlink the function from this module, returning
335// it. Because this removes the function from the module, the call graph node
336// is destroyed. This is only valid if the function does not call any other
337// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner25e9cad2001-11-26 18:51:25 +0000338// is to dropAllReferences before calling this.
339//
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000340Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
341 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
342 "graph if it references other functions!");
Chris Lattner5714c972003-08-31 20:36:52 +0000343 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000344 delete CGN; // Delete the call graph node for this func
Chris Lattner5714c972003-08-31 20:36:52 +0000345 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner25e9cad2001-11-26 18:51:25 +0000346
Chris Lattner5714c972003-08-31 20:36:52 +0000347 Mod->getFunctionList().remove(F);
348 return F;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000349}
350
Chris Lattner14fffaf2003-10-30 05:17:30 +0000351void CallGraph::stub() {}