1 //===- CallGraph.cpp - Build a Module's call graph ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the CallGraph class and provides the BasicCallGraph
11 // default implementation.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/CallGraph.h"
16 #include "llvm/Module.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/IntrinsicInst.h"
19 #include "llvm/Support/CallSite.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/raw_ostream.h"
26 //===----------------------------------------------------------------------===//
27 // BasicCallGraph class definition
29 class BasicCallGraph : public ModulePass, public CallGraph {
30 // Root is root of the call graph, or the external node if a 'main' function
35 // ExternalCallingNode - This node has edges to all external functions and
36 // those internal functions that have their address taken.
37 CallGraphNode *ExternalCallingNode;
39 // CallsExternalNode - This node has edges to it from all functions making
40 // indirect calls or calling an external function.
41 CallGraphNode *CallsExternalNode;
44 static char ID; // Class identification, replacement for typeinfo
45 BasicCallGraph() : ModulePass(&ID), Root(0),
46 ExternalCallingNode(0), CallsExternalNode(0) {}
48 // runOnModule - Compute the call graph for the specified module.
49 virtual bool runOnModule(Module &M) {
50 CallGraph::initialize(M);
52 ExternalCallingNode = getOrInsertFunction(0);
53 CallsExternalNode = new CallGraphNode(0);
56 // Add every function to the call graph.
57 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
60 // If we didn't find a main function, use the external call graph node
61 if (Root == 0) Root = ExternalCallingNode;
66 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
70 virtual void print(raw_ostream &OS, const Module *) const {
71 OS << "CallGraph Root is: ";
72 if (Function *F = getRoot()->getFunction())
73 OS << F->getName() << "\n";
75 OS << "<<null function: 0x" << getRoot() << ">>\n";
78 CallGraph::print(OS, 0);
81 virtual void releaseMemory() {
85 /// getAdjustedAnalysisPointer - This method is used when a pass implements
86 /// an analysis interface through multiple inheritance. If needed, it should
87 /// override this to adjust the this pointer as needed for the specified pass
89 virtual void *getAdjustedAnalysisPointer(const PassInfo *PI) {
90 if (PI->isPassID(&CallGraph::ID))
91 return (CallGraph*)this;
95 CallGraphNode* getExternalCallingNode() const { return ExternalCallingNode; }
96 CallGraphNode* getCallsExternalNode() const { return CallsExternalNode; }
98 // getRoot - Return the root of the call graph, which is either main, or if
99 // main cannot be found, the external node.
101 CallGraphNode *getRoot() { return Root; }
102 const CallGraphNode *getRoot() const { return Root; }
105 //===---------------------------------------------------------------------
106 // Implementation of CallGraph construction
109 // addToCallGraph - Add a function to the call graph, and link the node to all
110 // of the functions that it calls.
112 void addToCallGraph(Function *F) {
113 CallGraphNode *Node = getOrInsertFunction(F);
115 // If this function has external linkage, anything could call it.
116 if (!F->hasLocalLinkage()) {
117 ExternalCallingNode->addCalledFunction(CallSite(), Node);
119 // Found the entry point?
120 if (F->getName() == "main") {
121 if (Root) // Found multiple external mains? Don't pick one.
122 Root = ExternalCallingNode;
124 Root = Node; // Found a main, keep track of it!
128 // Loop over all of the users of the function, looking for non-call uses.
129 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I)
130 if ((!isa<CallInst>(I) && !isa<InvokeInst>(I))
131 || !CallSite(cast<Instruction>(I)).isCallee(I)) {
132 // Not a call, or being used as a parameter rather than as the callee.
133 ExternalCallingNode->addCalledFunction(CallSite(), Node);
137 // If this function is not defined in this translation unit, it could call
139 if (F->isDeclaration() && !F->isIntrinsic())
140 Node->addCalledFunction(CallSite(), CallsExternalNode);
142 // Look for calls by this function.
143 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
144 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
146 CallSite CS = CallSite::get(II);
147 if (CS.getInstruction() && !isa<DbgInfoIntrinsic>(II)) {
148 const Function *Callee = CS.getCalledFunction();
150 Node->addCalledFunction(CS, getOrInsertFunction(Callee));
152 Node->addCalledFunction(CS, CallsExternalNode);
158 // destroy - Release memory for the call graph
159 virtual void destroy() {
160 /// CallsExternalNode is not in the function map, delete it explicitly.
161 if (CallsExternalNode) {
162 CallsExternalNode->allReferencesDropped();
163 delete CallsExternalNode;
164 CallsExternalNode = 0;
166 CallGraph::destroy();
170 } //End anonymous namespace
172 static RegisterAnalysisGroup<CallGraph> X("Call Graph");
173 static RegisterPass<BasicCallGraph>
174 Y("basiccg", "Basic CallGraph Construction", false, true);
175 static RegisterAnalysisGroup<CallGraph, true> Z(Y);
177 char CallGraph::ID = 0;
178 char BasicCallGraph::ID = 0;
180 void CallGraph::initialize(Module &M) {
184 void CallGraph::destroy() {
185 if (FunctionMap.empty()) return;
187 // Reset all node's use counts to zero before deleting them to prevent an
188 // assertion from firing.
190 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
192 I->second->allReferencesDropped();
195 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
201 void CallGraph::print(raw_ostream &OS, Module*) const {
202 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
203 I->second->print(OS);
205 void CallGraph::dump() const {
209 //===----------------------------------------------------------------------===//
210 // Implementations of public modification methods
213 // removeFunctionFromModule - Unlink the function from this module, returning
214 // it. Because this removes the function from the module, the call graph node
215 // is destroyed. This is only valid if the function does not call any other
216 // functions (ie, there are no edges in it's CGN). The easiest way to do this
217 // is to dropAllReferences before calling this.
219 Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
220 assert(CGN->empty() && "Cannot remove function from call "
221 "graph if it references other functions!");
222 Function *F = CGN->getFunction(); // Get the function for the call graph node
223 delete CGN; // Delete the call graph node for this func
224 FunctionMap.erase(F); // Remove the call graph node from the map
226 Mod->getFunctionList().remove(F);
230 // getOrInsertFunction - This method is identical to calling operator[], but
231 // it will insert a new CallGraphNode for the specified function if one does
232 // not already exist.
233 CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
234 CallGraphNode *&CGN = FunctionMap[F];
237 assert((!F || F->getParent() == Mod) && "Function not in current module!");
238 return CGN = new CallGraphNode(const_cast<Function*>(F));
241 void CallGraphNode::print(raw_ostream &OS) const {
242 if (Function *F = getFunction())
243 OS << "Call graph node for function: '" << F->getName() << "'";
245 OS << "Call graph node <<null function>>";
247 OS << "<<" << this << ">> #uses=" << getNumReferences() << '\n';
249 for (const_iterator I = begin(), E = end(); I != E; ++I) {
250 OS << " CS<" << I->first << "> calls ";
251 if (Function *FI = I->second->getFunction())
252 OS << "function '" << FI->getName() <<"'\n";
254 OS << "external node\n";
259 void CallGraphNode::dump() const { print(dbgs()); }
261 /// removeCallEdgeFor - This method removes the edge in the node for the
262 /// specified call site. Note that this method takes linear time, so it
263 /// should be used sparingly.
264 void CallGraphNode::removeCallEdgeFor(CallSite CS) {
265 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
266 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
267 if (I->first == CS.getInstruction()) {
268 I->second->DropRef();
269 *I = CalledFunctions.back();
270 CalledFunctions.pop_back();
277 // removeAnyCallEdgeTo - This method removes any call edges from this node to
278 // the specified callee function. This takes more time to execute than
279 // removeCallEdgeTo, so it should not be used unless necessary.
280 void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
281 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
282 if (CalledFunctions[i].second == Callee) {
284 CalledFunctions[i] = CalledFunctions.back();
285 CalledFunctions.pop_back();
290 /// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite
291 /// from this node to the specified callee function.
292 void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) {
293 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
294 assert(I != CalledFunctions.end() && "Cannot find callee to remove!");
296 if (CR.second == Callee && CR.first == 0) {
298 *I = CalledFunctions.back();
299 CalledFunctions.pop_back();
305 /// replaceCallEdge - This method replaces the edge in the node for the
306 /// specified call site with a new one. Note that this method takes linear
307 /// time, so it should be used sparingly.
308 void CallGraphNode::replaceCallEdge(CallSite CS,
309 CallSite NewCS, CallGraphNode *NewNode){
310 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
311 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
312 if (I->first == CS.getInstruction()) {
313 I->second->DropRef();
314 I->first = NewCS.getInstruction();
322 // Enuse that users of CallGraph.h also link with this file
323 DEFINING_FILE_FOR(CallGraph)