1 //===- CallGraph.cpp - Build a Module's call graph ------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This interface is used to build and manipulate a call graph, which is a very
11 // useful tool for interprocedural optimization.
13 // 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
15 // corresponding to the node.
17 // A call graph will contain nodes where the function that they correspond to is
18 // 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
20 // 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.
27 // Similarly, functions have a call edge to the external node iff:
28 // 1. The function is external, reflecting the fact that they could call
29 // anything without internal linkage or that has its address taken.
30 // 2. The function contains an indirect function call.
32 // As an extension in the future, there may be multiple nodes with a null
33 // 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.
36 // Because of these properties, the CallGraph captures a conservative superset
37 // of all of the caller-callee relationships, which is useful for
40 // The CallGraph class also attempts to figure out what the root of the
41 // 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
44 // be called into (which is common for libraries).
46 //===----------------------------------------------------------------------===//
48 #include "llvm/Analysis/CallGraph.h"
49 #include "llvm/Constants.h" // Remove when ConstantPointerRefs are gone
50 #include "llvm/Module.h"
51 #include "llvm/iOther.h"
52 #include "llvm/iTerminators.h"
53 #include "llvm/Support/CallSite.h"
54 #include "Support/STLExtras.h"
58 static RegisterAnalysis<CallGraph> X("callgraph", "Call Graph Construction");
60 static const char * const KnownExternalFunctions[] = {
61 // Low-level system calls
70 // Low-level stdc library functions
74 // Standard IO functions
133 "exp", "sqrt", "cbrt", "hypot",
134 "log", "log10", "pow",
136 "asin", "acos", "atan", "atan2",
145 // gettext functions used by libstdc++
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
166 static bool ExternalFunctionDoesntCallIntoProgram(const std::string &Name) {
167 static std::vector<std::string> Funcs;
169 // First time this is called?
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());
179 if (Name.size() > 7 && !memcmp("__llvm_", Name.c_str(), 7))
182 // Binary search for the function name...
183 std::vector<std::string>::iterator I =
184 std::lower_bound(Funcs.begin(), Funcs.end(), Name);
187 return I != Funcs.end() && *I == Name;
192 // getNodeFor - Return the node for the specified function or create one if it
193 // does not already exist.
195 CallGraphNode *CallGraph::getNodeFor(Function *F) {
196 CallGraphNode *&CGN = FunctionMap[F];
199 assert((!F || F->getParent() == Mod) && "Function not in current module!");
200 return CGN = new CallGraphNode(F);
203 static 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;
210 // addToCallGraph - Add a function to the call graph, and link the node to all
211 // of the functions that it calls.
213 void CallGraph::addToCallGraph(Function *F) {
214 CallGraphNode *Node = getNodeFor(F);
216 // If this function has external linkage, anything could call it...
217 if (!F->hasInternalLinkage()) {
218 ExternalNode->addCalledFunction(Node);
220 // Found the entry point?
221 if (F->getName() == "main") {
223 Root = ExternalNode; // Found multiple external mains? Don't pick one.
225 Root = Node; // Found a main, keep track of it!
229 // If this function is not defined in this translation unit, it could call
231 if (F->isExternal() && !F->getIntrinsicID() &&
232 !ExternalFunctionDoesntCallIntoProgram(F->getName()))
233 Node->addCalledFunction(ExternalNode);
235 // Loop over all of the users of the function... looking for callers...
237 bool isUsedExternally = false;
238 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I) {
239 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
240 if (isOnlyADirectCall(F, CallSite::get(Inst)))
241 getNodeFor(Inst->getParent()->getParent())->addCalledFunction(Node);
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();
249 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
250 if (isOnlyADirectCall(F, CallSite::get(Inst)))
251 getNodeFor(Inst->getParent()->getParent())->addCalledFunction(Node);
253 isUsedExternally = true;
255 isUsedExternally = true;
257 } else { // Can't classify the user!
258 isUsedExternally = true;
261 if (isUsedExternally)
262 ExternalNode->addCalledFunction(Node);
264 // Look for an indirect function call...
265 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
266 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II){
267 CallSite CS = CallSite::get(II);
268 if (CS.getInstruction() && !CS.getCalledFunction())
269 Node->addCalledFunction(ExternalNode);
273 bool CallGraph::run(Module &M) {
277 ExternalNode = getNodeFor(0);
280 // Add every function to the call graph...
281 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
284 // If we didn't find a main function, use the external call graph node
285 if (Root == 0) Root = ExternalNode;
290 void CallGraph::destroy() {
291 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
297 static void WriteToOutput(const CallGraphNode *CGN, std::ostream &o) {
298 if (CGN->getFunction())
299 o << "Call graph node for function: '"
300 << CGN->getFunction()->getName() <<"'\n";
302 o << "Call graph node <<null function: 0x" << CGN << ">>:\n";
304 for (unsigned i = 0; i < CGN->size(); ++i)
305 if ((*CGN)[i]->getFunction())
306 o << " Calls function '" << (*CGN)[i]->getFunction()->getName() << "'\n";
308 o << " Calls external node\n";
312 void CallGraph::print(std::ostream &o, const Module *M) const {
313 o << "CallGraph Root is: ";
314 if (getRoot()->getFunction())
315 o << getRoot()->getFunction()->getName() << "\n";
317 o << "<<null function: 0x" << getRoot() << ">>\n";
319 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
320 WriteToOutput(I->second, o);
324 //===----------------------------------------------------------------------===//
325 // Implementations of public modification methods
328 // Functions to keep a call graph up to date with a function that has been
331 void CallGraph::addFunctionToModule(Function *Meth) {
332 assert(0 && "not implemented");
336 // 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
340 // is to dropAllReferences before calling this.
342 Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
343 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
344 "graph if it references other functions!");
345 Function *F = CGN->getFunction(); // Get the function for the call graph node
346 delete CGN; // Delete the call graph node for this func
347 FunctionMap.erase(F); // Remove the call graph node from the map
349 Mod->getFunctionList().remove(F);
353 void CallGraph::stub() {}
355 } // End llvm namespace