blob: f5be91c8acfbf5c7464b329ce6f4dc96a1763bbf [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 Lattner41fbf302001-09-28 00:08:15 +000049#include "llvm/Module.h"
Chris Lattner41fbf302001-09-28 00:08:15 +000050#include "llvm/iOther.h"
Chris Lattner9f9e2be2001-10-13 06:33:19 +000051#include "llvm/iTerminators.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000052#include "Support/STLExtras.h"
Chris Lattner1e435162002-07-26 21:12:44 +000053
54static RegisterAnalysis<CallGraph> X("callgraph", "Call Graph Construction");
Chris Lattner93193f82002-01-31 00:42:27 +000055
Chris Lattnerb7c4c992003-10-22 18:53:31 +000056static const char * const KnownExternalFunctions[] = {
57 // Low-level system calls
58 "open",
59 "read",
60 "write",
61 "writev",
62 "lseek",
63 "poll",
64 "ioctl",
65
66 // Low-level stdc library functions
67 "abort",
68 "getenv",
69 "putenv",
70
71 // Standard IO functions
72 "printf",
73 "sprintf",
74 "fopen",
75 "freopen",
76 "fclose",
77 "fwrite",
78 "puts",
79 "fputs",
80 "getc",
81 "ungetc",
82 "putc",
83 "putchar",
84 "fread",
85 "fileno",
86 "ftell",
87 "fflush",
88 "fseek",
89 "fileno",
90 "ferror",
91 "feof",
92 "fdopen",
93 "__fxstat",
94 "setbuf",
95 "setbuffer",
96 "etlinebuf",
97 "setvbuf",
98
99 // Memory functions
100 "malloc",
101 "free",
102 "realloc",
103 "calloc",
104 "memalign",
105
106 // String functions
107 "atoi",
108 "memmove",
109 "memset",
110 "memchr",
111 "memcmp",
112 "strchr",
113 "strncpy",
114 "strncmp",
115 "strcmp",
116 "__strcoll_l",
117 "__strxfrm_l",
118 "__strftime_l",
119 "__strtol_l",
120 "__strtoul_l",
121 "__strtoll_l",
122 "__strtoull_l",
123 "__strtof_l",
124 "__strtod_l",
125 "__strtold_l",
126
127 // Locale functions
128 "__uselocale",
129 "__newlocale",
130 "__freelocale",
131 "__duplocale",
132 "__nl_langinfo_l",
133
134 // gettext functions used by libstdc++
135 "gettext",
136 "dgettext",
137 "dcgettext",
138 "textdomain",
139 "bindtextdomain",
140
141 // Random stuff
142 "__assert_fail",
143 "__errno_location",
144};
145
146
147/// ExternalFunctionDoesntCallIntoProgram - This hack is used to indicate to the
148/// call graph that the specified external function is _KNOWN_ to not call back
149/// into the program. This is important, because otherwise functions which call
150/// "printf" for example, end up in a great big SCC that goes from the function
151/// through main.
152///
153static bool ExternalFunctionDoesntCallIntoProgram(const std::string &Name) {
154 static std::vector<std::string> Funcs;
155
156 // First time this is called?
157 if (Funcs.empty()) {
158 // Add a whole bunch of functions which are often used...
159 Funcs.insert(Funcs.end(), KnownExternalFunctions,
160 KnownExternalFunctions+
161 sizeof(KnownExternalFunctions)/sizeof(KnownExternalFunctions[0]));
162 // Sort the list for efficient access
163 std::sort(Funcs.begin(), Funcs.end());
164 }
165
166 // Binary search for the function name...
167 std::vector<std::string>::iterator I =
168 std::lower_bound(Funcs.begin(), Funcs.end(), Name);
169
170 // Found it?
171 return I != Funcs.end() && *I == Name;
172}
173
174
175
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000176// getNodeFor - Return the node for the specified function or create one if it
Chris Lattner41fbf302001-09-28 00:08:15 +0000177// does not already exist.
178//
Chris Lattnere590ff22002-03-26 17:55:33 +0000179CallGraphNode *CallGraph::getNodeFor(Function *F) {
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000180 CallGraphNode *&CGN = FunctionMap[F];
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000181 if (CGN) return CGN;
Chris Lattner41fbf302001-09-28 00:08:15 +0000182
Chris Lattnere590ff22002-03-26 17:55:33 +0000183 assert((!F || F->getParent() == Mod) && "Function not in current module!");
184 return CGN = new CallGraphNode(F);
Chris Lattner41fbf302001-09-28 00:08:15 +0000185}
186
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000187// addToCallGraph - Add a function to the call graph, and link the node to all
188// of the functions that it calls.
Chris Lattner41fbf302001-09-28 00:08:15 +0000189//
Chris Lattner5714c972003-08-31 20:36:52 +0000190void CallGraph::addToCallGraph(Function *F) {
191 CallGraphNode *Node = getNodeFor(F);
Chris Lattner41fbf302001-09-28 00:08:15 +0000192
Chris Lattnerf52d01b2003-09-15 04:35:16 +0000193 // If this function has external linkage, anything could call it...
Chris Lattner5714c972003-08-31 20:36:52 +0000194 if (!F->hasInternalLinkage()) {
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000195 ExternalNode->addCalledFunction(Node);
Chris Lattner25e9cad2001-11-26 18:51:25 +0000196
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000197 // Found the entry point?
Chris Lattner5714c972003-08-31 20:36:52 +0000198 if (F->getName() == "main") {
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000199 if (Root)
200 Root = ExternalNode; // Found multiple external mains? Don't pick one.
201 else
202 Root = Node; // Found a main, keep track of it!
203 }
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000204 }
Chris Lattnerf52d01b2003-09-15 04:35:16 +0000205
206 // If this function is not defined in this translation unit, it could call
207 // anything.
Chris Lattnerb7c4c992003-10-22 18:53:31 +0000208 if (F->isExternal() && !F->getIntrinsicID() &&
209 !ExternalFunctionDoesntCallIntoProgram(F->getName()))
Chris Lattnerf52d01b2003-09-15 04:35:16 +0000210 Node->addCalledFunction(ExternalNode);
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000211
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000212 // Loop over all of the users of the function... looking for callers...
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000213 //
Chris Lattner5714c972003-08-31 20:36:52 +0000214 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I) {
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000215 User *U = *I;
216 if (CallInst *CI = dyn_cast<CallInst>(U))
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000217 getNodeFor(CI->getParent()->getParent())->addCalledFunction(Node);
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000218 else if (InvokeInst *II = dyn_cast<InvokeInst>(U))
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000219 getNodeFor(II->getParent()->getParent())->addCalledFunction(Node);
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000220 else // Can't classify the user!
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000221 ExternalNode->addCalledFunction(Node);
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000222 }
223
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000224 // Look for an indirect function call...
Chris Lattner5714c972003-08-31 20:36:52 +0000225 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000226 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II){
Chris Lattner7e708292002-06-25 16:13:24 +0000227 Instruction &I = *II;
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000228
Chris Lattner7e708292002-06-25 16:13:24 +0000229 if (CallInst *CI = dyn_cast<CallInst>(&I)) {
Chris Lattnerdc89f872002-03-29 17:08:29 +0000230 if (CI->getCalledFunction() == 0)
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000231 Node->addCalledFunction(ExternalNode);
Chris Lattner7e708292002-06-25 16:13:24 +0000232 } else if (InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattnerdc89f872002-03-29 17:08:29 +0000233 if (II->getCalledFunction() == 0)
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000234 Node->addCalledFunction(ExternalNode);
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000235 }
236 }
Chris Lattner41fbf302001-09-28 00:08:15 +0000237}
238
Chris Lattner7e708292002-06-25 16:13:24 +0000239bool CallGraph::run(Module &M) {
Chris Lattner93193f82002-01-31 00:42:27 +0000240 destroy();
241
Chris Lattner7e708292002-06-25 16:13:24 +0000242 Mod = &M;
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000243 ExternalNode = getNodeFor(0);
244 Root = 0;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000245
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000246 // Add every function to the call graph...
Chris Lattner7e708292002-06-25 16:13:24 +0000247 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
248 addToCallGraph(I);
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000249
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000250 // If we didn't find a main function, use the external call graph node
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000251 if (Root == 0) Root = ExternalNode;
Chris Lattner93193f82002-01-31 00:42:27 +0000252
253 return false;
Chris Lattner41fbf302001-09-28 00:08:15 +0000254}
255
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000256void CallGraph::destroy() {
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000257 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000258 I != E; ++I)
Chris Lattner25e9cad2001-11-26 18:51:25 +0000259 delete I->second;
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000260 FunctionMap.clear();
Chris Lattner25e9cad2001-11-26 18:51:25 +0000261}
262
Chris Lattner16500152002-11-04 00:21:19 +0000263static void WriteToOutput(const CallGraphNode *CGN, std::ostream &o) {
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000264 if (CGN->getFunction())
265 o << "Call graph node for function: '"
266 << CGN->getFunction()->getName() <<"'\n";
Chris Lattner25e9cad2001-11-26 18:51:25 +0000267 else
Chris Lattnerb31247a2003-09-15 04:29:37 +0000268 o << "Call graph node <<null function: 0x" << CGN << ">>:\n";
Chris Lattner25e9cad2001-11-26 18:51:25 +0000269
Chris Lattner41fbf302001-09-28 00:08:15 +0000270 for (unsigned i = 0; i < CGN->size(); ++i)
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000271 if ((*CGN)[i]->getFunction())
272 o << " Calls function '" << (*CGN)[i]->getFunction()->getName() << "'\n";
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000273 else
274 o << " Calls external node\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000275 o << "\n";
Chris Lattner41fbf302001-09-28 00:08:15 +0000276}
277
Chris Lattner16500152002-11-04 00:21:19 +0000278void CallGraph::print(std::ostream &o, const Module *M) const {
Chris Lattnerb31247a2003-09-15 04:29:37 +0000279 o << "CallGraph Root is: ";
280 if (getRoot()->getFunction())
281 o << getRoot()->getFunction()->getName() << "\n";
282 else
283 o << "<<null function: 0x" << getRoot() << ">>\n";
284
Chris Lattner16500152002-11-04 00:21:19 +0000285 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
286 WriteToOutput(I->second, o);
Chris Lattner41fbf302001-09-28 00:08:15 +0000287}
Vikram S. Advea7edb182001-10-22 13:55:46 +0000288
289
Chris Lattner25e9cad2001-11-26 18:51:25 +0000290//===----------------------------------------------------------------------===//
291// Implementations of public modification methods
292//
293
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000294// Functions to keep a call graph up to date with a function that has been
Chris Lattner25e9cad2001-11-26 18:51:25 +0000295// modified
296//
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000297void CallGraph::addFunctionToModule(Function *Meth) {
Chris Lattner25e9cad2001-11-26 18:51:25 +0000298 assert(0 && "not implemented");
299 abort();
300}
301
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000302// removeFunctionFromModule - Unlink the function from this module, returning
303// it. Because this removes the function from the module, the call graph node
304// is destroyed. This is only valid if the function does not call any other
305// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner25e9cad2001-11-26 18:51:25 +0000306// is to dropAllReferences before calling this.
307//
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000308Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
309 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
310 "graph if it references other functions!");
Chris Lattner5714c972003-08-31 20:36:52 +0000311 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000312 delete CGN; // Delete the call graph node for this func
Chris Lattner5714c972003-08-31 20:36:52 +0000313 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner25e9cad2001-11-26 18:51:25 +0000314
Chris Lattner5714c972003-08-31 20:36:52 +0000315 Mod->getFunctionList().remove(F);
316 return F;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000317}
318
Chris Lattner14fffaf2003-10-30 05:17:30 +0000319void CallGraph::stub() {}