blob: dfb5de68de0ccf0f4831895fc8349339dc56a543 [file] [log] [blame]
Chris Lattnerd852cc32002-03-06 18:00:49 +00001//===- CallGraph.cpp - Build a Module's call graph ------------------------===//
Chris Lattnerbbf3ae82001-09-28 00:08:15 +00002//
Chris Lattnerbeed7422002-03-06 20:19:35 +00003// This interface is used to build and manipulate a call graph, which is a very
4// useful tool for interprocedural optimization.
Chris Lattnerbbf3ae82001-09-28 00:08:15 +00005//
Chris Lattner79b0c7d2002-07-18 04:43:16 +00006// Every function in a module is represented as a node in the call graph. The
7// callgraph node keeps track of which functions the are called by the function
Chris Lattnerbeed7422002-03-06 20:19:35 +00008// corresponding to the node.
9//
Chris Lattner79b0c7d2002-07-18 04:43:16 +000010// A call graph will contain nodes where the function that they correspond to is
Chris Lattnerbeed7422002-03-06 20:19:35 +000011// null. This 'external' node is used to represent control flow that is not
12// represented (or analyzable) in the module. As such, the external node will
Chris Lattner79b0c7d2002-07-18 04:43:16 +000013// have edges to functions with the following properties:
14// 1. All functions in the module without internal linkage, since they could
15// be called by functions outside of the our analysis capability.
16// 2. All functions whose address is used for something more than a direct
17// call, for example being stored into a memory location. Since they may
18// be called by an unknown caller later, they must be tracked as such.
Chris Lattnerbeed7422002-03-06 20:19:35 +000019//
Chris Lattner79b0c7d2002-07-18 04:43:16 +000020// Similarly, functions have a call edge to the external node iff:
21// 1. The function is external, reflecting the fact that they could call
Chris Lattnerbeed7422002-03-06 20:19:35 +000022// anything without internal linkage or that has its address taken.
Chris Lattner79b0c7d2002-07-18 04:43:16 +000023// 2. The function contains an indirect function call.
Chris Lattnerbeed7422002-03-06 20:19:35 +000024//
25// As an extension in the future, there may be multiple nodes with a null
Chris Lattner79b0c7d2002-07-18 04:43:16 +000026// function. These will be used when we can prove (through pointer analysis)
27// that an indirect call site can call only a specific set of functions.
Chris Lattnerbeed7422002-03-06 20:19:35 +000028//
29// Because of these properties, the CallGraph captures a conservative superset
30// of all of the caller-callee relationships, which is useful for
31// transformations.
32//
33// The CallGraph class also attempts to figure out what the root of the
Chris Lattner79b0c7d2002-07-18 04:43:16 +000034// CallGraph is, which is currently does by looking for a function named 'main'.
35// If no function named 'main' is found, the external node is used as the entry
36// node, reflecting the fact that any function without internal linkage could
Chris Lattnerbeed7422002-03-06 20:19:35 +000037// be called into (which is common for libraries).
Chris Lattner8cbbbef2001-10-13 06:33:19 +000038//
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000039//===----------------------------------------------------------------------===//
40
41#include "llvm/Analysis/CallGraph.h"
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000042#include "llvm/Module.h"
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000043#include "llvm/iOther.h"
Chris Lattner8cbbbef2001-10-13 06:33:19 +000044#include "llvm/iTerminators.h"
Chris Lattner5de22042001-11-27 00:03:19 +000045#include "Support/STLExtras.h"
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000046#include <algorithm>
Chris Lattnera2c09852002-07-26 21:12:44 +000047
48static RegisterAnalysis<CallGraph> X("callgraph", "Call Graph Construction");
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000049
Chris Lattner80327322002-03-06 17:16:43 +000050AnalysisID CallGraph::ID(AnalysisID::create<CallGraph>());
Chris Lattnerccf571a2002-01-31 00:42:27 +000051
Chris Lattner79b0c7d2002-07-18 04:43:16 +000052// getNodeFor - Return the node for the specified function or create one if it
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000053// does not already exist.
54//
Chris Lattner0df67e32002-03-26 17:55:33 +000055CallGraphNode *CallGraph::getNodeFor(Function *F) {
Chris Lattner79b0c7d2002-07-18 04:43:16 +000056 CallGraphNode *&CGN = FunctionMap[F];
Chris Lattnerbeed7422002-03-06 20:19:35 +000057 if (CGN) return CGN;
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000058
Chris Lattner0df67e32002-03-26 17:55:33 +000059 assert((!F || F->getParent() == Mod) && "Function not in current module!");
60 return CGN = new CallGraphNode(F);
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000061}
62
Chris Lattner79b0c7d2002-07-18 04:43:16 +000063// addToCallGraph - Add a function to the call graph, and link the node to all
64// of the functions that it calls.
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000065//
Chris Lattner0df67e32002-03-26 17:55:33 +000066void CallGraph::addToCallGraph(Function *M) {
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000067 CallGraphNode *Node = getNodeFor(M);
68
Chris Lattner79b0c7d2002-07-18 04:43:16 +000069 // If this function has external linkage,
Chris Lattnerbeed7422002-03-06 20:19:35 +000070 if (!M->hasInternalLinkage()) {
Chris Lattner79b0c7d2002-07-18 04:43:16 +000071 ExternalNode->addCalledFunction(Node);
Chris Lattner03946cd2001-11-26 18:51:25 +000072
Chris Lattnerbeed7422002-03-06 20:19:35 +000073 // Found the entry point?
74 if (M->getName() == "main") {
75 if (Root)
76 Root = ExternalNode; // Found multiple external mains? Don't pick one.
77 else
78 Root = Node; // Found a main, keep track of it!
79 }
80 } else if (M->isExternal()) { // Not defined in this xlation unit?
Chris Lattner79b0c7d2002-07-18 04:43:16 +000081 Node->addCalledFunction(ExternalNode); // It could call anything...
Chris Lattnerbeed7422002-03-06 20:19:35 +000082 }
83
Chris Lattner79b0c7d2002-07-18 04:43:16 +000084 // Loop over all of the users of the function... looking for callers...
Chris Lattnerbeed7422002-03-06 20:19:35 +000085 //
86 for (Value::use_iterator I = M->use_begin(), E = M->use_end(); I != E; ++I) {
87 User *U = *I;
88 if (CallInst *CI = dyn_cast<CallInst>(U))
Chris Lattner79b0c7d2002-07-18 04:43:16 +000089 getNodeFor(CI->getParent()->getParent())->addCalledFunction(Node);
Chris Lattnerbeed7422002-03-06 20:19:35 +000090 else if (InvokeInst *II = dyn_cast<InvokeInst>(U))
Chris Lattner79b0c7d2002-07-18 04:43:16 +000091 getNodeFor(II->getParent()->getParent())->addCalledFunction(Node);
Chris Lattnerbeed7422002-03-06 20:19:35 +000092 else // Can't classify the user!
Chris Lattner79b0c7d2002-07-18 04:43:16 +000093 ExternalNode->addCalledFunction(Node);
Chris Lattnerbeed7422002-03-06 20:19:35 +000094 }
95
Chris Lattner79b0c7d2002-07-18 04:43:16 +000096 // Look for an indirect function call...
Chris Lattner113f4f42002-06-25 16:13:24 +000097 for (Function::iterator BB = M->begin(), BBE = M->end(); BB != BBE; ++BB)
Chris Lattnerbeed7422002-03-06 20:19:35 +000098 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II){
Chris Lattner113f4f42002-06-25 16:13:24 +000099 Instruction &I = *II;
Chris Lattnerbeed7422002-03-06 20:19:35 +0000100
Chris Lattner113f4f42002-06-25 16:13:24 +0000101 if (CallInst *CI = dyn_cast<CallInst>(&I)) {
Chris Lattner15deaa02002-03-29 17:08:29 +0000102 if (CI->getCalledFunction() == 0)
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000103 Node->addCalledFunction(ExternalNode);
Chris Lattner113f4f42002-06-25 16:13:24 +0000104 } else if (InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
Chris Lattner15deaa02002-03-29 17:08:29 +0000105 if (II->getCalledFunction() == 0)
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000106 Node->addCalledFunction(ExternalNode);
Chris Lattnerbeed7422002-03-06 20:19:35 +0000107 }
108 }
Chris Lattnerbbf3ae82001-09-28 00:08:15 +0000109}
110
Chris Lattner113f4f42002-06-25 16:13:24 +0000111bool CallGraph::run(Module &M) {
Chris Lattnerccf571a2002-01-31 00:42:27 +0000112 destroy();
113
Chris Lattner113f4f42002-06-25 16:13:24 +0000114 Mod = &M;
Chris Lattnerbeed7422002-03-06 20:19:35 +0000115 ExternalNode = getNodeFor(0);
116 Root = 0;
Chris Lattner03946cd2001-11-26 18:51:25 +0000117
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000118 // Add every function to the call graph...
Chris Lattner113f4f42002-06-25 16:13:24 +0000119 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
120 addToCallGraph(I);
Chris Lattnerbeed7422002-03-06 20:19:35 +0000121
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000122 // If we didn't find a main function, use the external call graph node
Chris Lattnerbeed7422002-03-06 20:19:35 +0000123 if (Root == 0) Root = ExternalNode;
Chris Lattnerccf571a2002-01-31 00:42:27 +0000124
125 return false;
Chris Lattnerbbf3ae82001-09-28 00:08:15 +0000126}
127
Chris Lattner80327322002-03-06 17:16:43 +0000128void CallGraph::destroy() {
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000129 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
Chris Lattnerbeed7422002-03-06 20:19:35 +0000130 I != E; ++I)
Chris Lattner03946cd2001-11-26 18:51:25 +0000131 delete I->second;
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000132 FunctionMap.clear();
Chris Lattner03946cd2001-11-26 18:51:25 +0000133}
134
Chris Lattnerbbf3ae82001-09-28 00:08:15 +0000135
Chris Lattner80327322002-03-06 17:16:43 +0000136void WriteToOutput(const CallGraphNode *CGN, std::ostream &o) {
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000137 if (CGN->getFunction())
138 o << "Call graph node for function: '"
139 << CGN->getFunction()->getName() <<"'\n";
Chris Lattner03946cd2001-11-26 18:51:25 +0000140 else
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000141 o << "Call graph node null function:\n";
Chris Lattner03946cd2001-11-26 18:51:25 +0000142
Chris Lattnerbbf3ae82001-09-28 00:08:15 +0000143 for (unsigned i = 0; i < CGN->size(); ++i)
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000144 if ((*CGN)[i]->getFunction())
145 o << " Calls function '" << (*CGN)[i]->getFunction()->getName() << "'\n";
Chris Lattnerbeed7422002-03-06 20:19:35 +0000146 else
147 o << " Calls external node\n";
Chris Lattner7f74a562002-01-20 22:54:45 +0000148 o << "\n";
Chris Lattnerbbf3ae82001-09-28 00:08:15 +0000149}
150
Chris Lattner80327322002-03-06 17:16:43 +0000151void WriteToOutput(const CallGraph &CG, std::ostream &o) {
Chris Lattnerac7c2982002-04-10 20:31:44 +0000152 o << "CallGraph Root is:\n" << CG.getRoot();
153
Chris Lattnerbbf3ae82001-09-28 00:08:15 +0000154 for (CallGraph::const_iterator I = CG.begin(), E = CG.end(); I != E; ++I)
155 o << I->second;
156}
Vikram S. Adve5dab57d2001-10-22 13:55:46 +0000157
158
Chris Lattner03946cd2001-11-26 18:51:25 +0000159//===----------------------------------------------------------------------===//
160// Implementations of public modification methods
161//
162
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000163// Functions to keep a call graph up to date with a function that has been
Chris Lattner03946cd2001-11-26 18:51:25 +0000164// modified
165//
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000166void CallGraph::addFunctionToModule(Function *Meth) {
Chris Lattner03946cd2001-11-26 18:51:25 +0000167 assert(0 && "not implemented");
168 abort();
169}
170
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000171// removeFunctionFromModule - Unlink the function from this module, returning
172// it. Because this removes the function from the module, the call graph node
173// is destroyed. This is only valid if the function does not call any other
174// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner03946cd2001-11-26 18:51:25 +0000175// is to dropAllReferences before calling this.
176//
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000177Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
178 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
179 "graph if it references other functions!");
180 Function *M = CGN->getFunction(); // Get the function for the call graph node
181 delete CGN; // Delete the call graph node for this func
182 FunctionMap.erase(M); // Remove the call graph node from the map
Chris Lattner03946cd2001-11-26 18:51:25 +0000183
Chris Lattner0df67e32002-03-26 17:55:33 +0000184 Mod->getFunctionList().remove(M);
Chris Lattner03946cd2001-11-26 18:51:25 +0000185 return M;
186}
187