blob: 23a7599ce57138da2d9199fe3d32a7ff5a9770d4 [file] [log] [blame]
Chris Lattner8d5a16c2002-03-06 18:00:49 +00001//===- CallGraph.cpp - Build a Module's call graph ------------------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner41fbf302001-09-28 00:08:15 +00009//
Chris Lattner03839952005-12-22 06:07:52 +000010// This file implements the CallGraph class and provides the BasicCallGraph
11// default implementation.
Chris Lattner9f9e2be2001-10-13 06:33:19 +000012//
Chris Lattner41fbf302001-09-28 00:08:15 +000013//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/CallGraph.h"
Chris Lattner41fbf302001-09-28 00:08:15 +000016#include "llvm/Module.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000017#include "llvm/Instructions.h"
Chris Lattner07a38e72003-10-31 21:05:12 +000018#include "llvm/Support/CallSite.h"
Chris Lattneraf8a4242004-08-08 03:27:49 +000019#include <iostream>
Chris Lattnerb81c0212004-04-12 05:36:32 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Chris Lattner07a38e72003-10-31 21:05:12 +000022static bool isOnlyADirectCall(Function *F, CallSite CS) {
23 if (!CS.getInstruction()) return false;
24 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; ++I)
25 if (*I == F) return false;
26 return true;
27}
28
Chris Lattner03839952005-12-22 06:07:52 +000029namespace {
30
31//===----------------------------------------------------------------------===//
32// BasicCallGraph class definition
Chris Lattner41fbf302001-09-28 00:08:15 +000033//
Chris Lattner03839952005-12-22 06:07:52 +000034class BasicCallGraph : public CallGraph, public ModulePass {
35 // Root is root of the call graph, or the external node if a 'main' function
36 // couldn't be found.
37 //
38 CallGraphNode *Root;
Chris Lattner41fbf302001-09-28 00:08:15 +000039
Chris Lattner03839952005-12-22 06:07:52 +000040 // ExternalCallingNode - This node has edges to all external functions and
41 // those internal functions that have their address taken.
42 CallGraphNode *ExternalCallingNode;
Chris Lattner25e9cad2001-11-26 18:51:25 +000043
Chris Lattner03839952005-12-22 06:07:52 +000044 // CallsExternalNode - This node has edges to it from all functions making
45 // indirect calls or calling an external function.
46 CallGraphNode *CallsExternalNode;
47
48public:
49 BasicCallGraph() : Root(0), ExternalCallingNode(0), CallsExternalNode(0) {}
50 ~BasicCallGraph() { destroy(); }
51
52 // runOnModule - Compute the call graph for the specified module.
53 virtual bool runOnModule(Module &M) {
54 destroy();
55 CallGraph::initialize(M);
56
Chris Lattnerc54b1c12006-01-14 20:03:00 +000057 ExternalCallingNode = getOrInsertFunction(0);
Chris Lattner03839952005-12-22 06:07:52 +000058 CallsExternalNode = new CallGraphNode(0);
59 Root = 0;
60
61 // Add every function to the call graph...
62 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
63 addToCallGraph(I);
64
65 // If we didn't find a main function, use the external call graph node
66 if (Root == 0) Root = ExternalCallingNode;
67
68 return false;
Chris Lattnerd4d427b2002-03-06 20:19:35 +000069 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000070
Chris Lattner03839952005-12-22 06:07:52 +000071 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
72 AU.setPreservesAll();
73 }
Chris Lattnerd4d427b2002-03-06 20:19:35 +000074
Chris Lattner03839952005-12-22 06:07:52 +000075 virtual void print(std::ostream &o, const Module *M) const {
76 o << "CallGraph Root is: ";
77 if (Function *F = getRoot()->getFunction())
78 o << F->getName() << "\n";
79 else
80 o << "<<null function: 0x" << getRoot() << ">>\n";
81
82 CallGraph::print(o, M);
83 }
84
85 virtual void releaseMemory() {
86 destroy();
87 }
88
89 /// dump - Print out this call graph.
90 ///
91 inline void dump() const {
92 print(std::cerr, Mod);
93 }
94
95 CallGraphNode* getExternalCallingNode() const { return ExternalCallingNode; }
96 CallGraphNode* getCallsExternalNode() const { return CallsExternalNode; }
97
98 // getRoot - Return the root of the call graph, which is either main, or if
99 // main cannot be found, the external node.
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000100 //
Chris Lattner03839952005-12-22 06:07:52 +0000101 CallGraphNode *getRoot() { return Root; }
102 const CallGraphNode *getRoot() const { return Root; }
103
104private:
105 //===---------------------------------------------------------------------
106 // Implementation of CallGraph construction
107 //
Chris Lattner03839952005-12-22 06:07:52 +0000108
Chris Lattner03839952005-12-22 06:07:52 +0000109 // addToCallGraph - Add a function to the call graph, and link the node to all
110 // of the functions that it calls.
111 //
112 void addToCallGraph(Function *F) {
Chris Lattnerc54b1c12006-01-14 20:03:00 +0000113 CallGraphNode *Node = getOrInsertFunction(F);
Chris Lattner03839952005-12-22 06:07:52 +0000114
Chris Lattnerd85340f2006-07-12 18:29:36 +0000115 // If this function has external linkage, anything could call it.
Chris Lattner03839952005-12-22 06:07:52 +0000116 if (!F->hasInternalLinkage()) {
Chris Lattnerd85340f2006-07-12 18:29:36 +0000117 ExternalCallingNode->addCalledFunction(CallSite(), Node);
Chris Lattner03839952005-12-22 06:07:52 +0000118
119 // Found the entry point?
120 if (F->getName() == "main") {
121 if (Root) // Found multiple external mains? Don't pick one.
122 Root = ExternalCallingNode;
123 else
124 Root = Node; // Found a main, keep track of it!
125 }
126 }
127
128 // If this function is not defined in this translation unit, it could call
129 // anything.
130 if (F->isExternal() && !F->getIntrinsicID())
Chris Lattnerd85340f2006-07-12 18:29:36 +0000131 Node->addCalledFunction(CallSite(), CallsExternalNode);
Chris Lattner03839952005-12-22 06:07:52 +0000132
133 // Loop over all of the users of the function... looking for callers...
134 //
135 bool isUsedExternally = false;
136 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I){
137 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
Chris Lattnerd85340f2006-07-12 18:29:36 +0000138 CallSite CS = CallSite::get(Inst);
139 if (isOnlyADirectCall(F, CS))
Chris Lattnerc54b1c12006-01-14 20:03:00 +0000140 getOrInsertFunction(Inst->getParent()->getParent())
Chris Lattnerd85340f2006-07-12 18:29:36 +0000141 ->addCalledFunction(CS, Node);
Chris Lattner03839952005-12-22 06:07:52 +0000142 else
143 isUsedExternally = true;
144 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(*I)) {
145 for (Value::use_iterator I = GV->use_begin(), E = GV->use_end();
146 I != E; ++I)
147 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
Chris Lattnerd85340f2006-07-12 18:29:36 +0000148 CallSite CS = CallSite::get(Inst);
149 if (isOnlyADirectCall(F, CS))
150 getOrInsertFunction(Inst->getParent()->getParent())
151 ->addCalledFunction(CS, Node);
152 else
153 isUsedExternally = true;
Chris Lattner03839952005-12-22 06:07:52 +0000154 } else {
155 isUsedExternally = true;
156 }
157 } else { // Can't classify the user!
158 isUsedExternally = true;
159 }
Chris Lattner07a38e72003-10-31 21:05:12 +0000160 }
Chris Lattner03839952005-12-22 06:07:52 +0000161 if (isUsedExternally)
Chris Lattnerd85340f2006-07-12 18:29:36 +0000162 ExternalCallingNode->addCalledFunction(CallSite(), Node);
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000163
Chris Lattnerd85340f2006-07-12 18:29:36 +0000164 // Look for an indirect function call.
Chris Lattner03839952005-12-22 06:07:52 +0000165 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
166 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
167 II != IE; ++II) {
Chris Lattner07a38e72003-10-31 21:05:12 +0000168 CallSite CS = CallSite::get(II);
169 if (CS.getInstruction() && !CS.getCalledFunction())
Chris Lattnerd85340f2006-07-12 18:29:36 +0000170 Node->addCalledFunction(CS, CallsExternalNode);
Chris Lattner03839952005-12-22 06:07:52 +0000171 }
172 }
173
174 //
175 // destroy - Release memory for the call graph
176 virtual void destroy() {
177 if (!CallsExternalNode) {
178 delete CallsExternalNode;
179 CallsExternalNode = 0;
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000180 }
Chris Lattner03839952005-12-22 06:07:52 +0000181 }
182};
Chris Lattner41fbf302001-09-28 00:08:15 +0000183
Chris Lattner03839952005-12-22 06:07:52 +0000184RegisterAnalysisGroup<CallGraph> X("Call Graph");
185RegisterOpt<BasicCallGraph> Y("basiccg", "Basic CallGraph Construction");
186RegisterAnalysisGroup<CallGraph, BasicCallGraph, true> Z;
187
188} //End anonymous namespace
189
190void CallGraph::initialize(Module &M) {
Chris Lattner93193f82002-01-31 00:42:27 +0000191 destroy();
Chris Lattner7e708292002-06-25 16:13:24 +0000192 Mod = &M;
Chris Lattner41fbf302001-09-28 00:08:15 +0000193}
194
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000195void CallGraph::destroy() {
Chris Lattner03839952005-12-22 06:07:52 +0000196 if(!FunctionMap.size()) {
197 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
198 I != E; ++I)
199 delete I->second;
200 FunctionMap.clear();
201 }
Chris Lattner25e9cad2001-11-26 18:51:25 +0000202}
203
Chris Lattneraf8a4242004-08-08 03:27:49 +0000204void CallGraph::print(std::ostream &OS, const Module *M) const {
Chris Lattner16500152002-11-04 00:21:19 +0000205 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattneraf8a4242004-08-08 03:27:49 +0000206 I->second->print(OS);
207}
208
Chris Lattner1694ec62006-01-14 19:17:02 +0000209void CallGraph::dump() const {
210 print(std::cerr, 0);
211}
212
Chris Lattner25e9cad2001-11-26 18:51:25 +0000213//===----------------------------------------------------------------------===//
214// Implementations of public modification methods
215//
216
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000217// removeFunctionFromModule - Unlink the function from this module, returning
218// it. Because this removes the function from the module, the call graph node
219// is destroyed. This is only valid if the function does not call any other
220// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner25e9cad2001-11-26 18:51:25 +0000221// is to dropAllReferences before calling this.
222//
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000223Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
224 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
225 "graph if it references other functions!");
Chris Lattner5714c972003-08-31 20:36:52 +0000226 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000227 delete CGN; // Delete the call graph node for this func
Chris Lattner5714c972003-08-31 20:36:52 +0000228 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner25e9cad2001-11-26 18:51:25 +0000229
Chris Lattner5714c972003-08-31 20:36:52 +0000230 Mod->getFunctionList().remove(F);
231 return F;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000232}
233
Chris Lattner6f7e5eb2004-09-18 00:22:13 +0000234// changeFunction - This method changes the function associated with this
235// CallGraphNode, for use by transformations that need to change the prototype
236// of a Function (thus they must create a new Function and move the old code
237// over).
238void CallGraph::changeFunction(Function *OldF, Function *NewF) {
239 iterator I = FunctionMap.find(OldF);
240 CallGraphNode *&New = FunctionMap[NewF];
241 assert(I != FunctionMap.end() && I->second && !New &&
242 "OldF didn't exist in CG or NewF already does!");
243 New = I->second;
Chris Lattner3795bc92004-09-18 00:27:20 +0000244 New->F = NewF;
Chris Lattner6f7e5eb2004-09-18 00:22:13 +0000245 FunctionMap.erase(I);
246}
247
Chris Lattnerc54b1c12006-01-14 20:03:00 +0000248// getOrInsertFunction - This method is identical to calling operator[], but
249// it will insert a new CallGraphNode for the specified function if one does
250// not already exist.
251CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
252 CallGraphNode *&CGN = FunctionMap[F];
253 if (CGN) return CGN;
254
255 assert((!F || F->getParent() == Mod) && "Function not in current module!");
256 return CGN = new CallGraphNode(const_cast<Function*>(F));
257}
258
Chris Lattner03839952005-12-22 06:07:52 +0000259void CallGraphNode::print(std::ostream &OS) const {
260 if (Function *F = getFunction())
261 OS << "Call graph node for function: '" << F->getName() <<"'\n";
262 else
263 OS << "Call graph node <<null function: 0x" << this << ">>:\n";
264
265 for (const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattnerd85340f2006-07-12 18:29:36 +0000266 if (I->second->getFunction())
267 OS << " Calls function '" << I->second->getFunction()->getName() <<"'\n";
Chris Lattner03839952005-12-22 06:07:52 +0000268 else
269 OS << " Calls external node\n";
270 OS << "\n";
271}
272
273void CallGraphNode::dump() const { print(std::cerr); }
274
Chris Lattnerb81c0212004-04-12 05:36:32 +0000275void CallGraphNode::removeCallEdgeTo(CallGraphNode *Callee) {
276 for (unsigned i = CalledFunctions.size(); ; --i) {
277 assert(i && "Cannot find callee to remove!");
Chris Lattnerd85340f2006-07-12 18:29:36 +0000278 if (CalledFunctions[i-1].second == Callee) {
Chris Lattnerb81c0212004-04-12 05:36:32 +0000279 CalledFunctions.erase(CalledFunctions.begin()+i-1);
280 return;
281 }
282 }
283}
Chris Lattnercd382a32004-09-18 21:34:34 +0000284
285// removeAnyCallEdgeTo - This method removes any call edges from this node to
286// the specified callee function. This takes more time to execute than
287// removeCallEdgeTo, so it should not be used unless necessary.
288void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
Chris Lattnerfff03c92004-09-19 19:01:06 +0000289 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
Chris Lattnerd85340f2006-07-12 18:29:36 +0000290 if (CalledFunctions[i].second == Callee) {
Chris Lattnerfff03c92004-09-19 19:01:06 +0000291 CalledFunctions[i] = CalledFunctions.back();
292 CalledFunctions.pop_back();
293 --i; --e;
Chris Lattnercd382a32004-09-18 21:34:34 +0000294 }
295}
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000296
297// Enuse that users of CallGraph.h also link with this file
298DEFINING_FILE_FOR(CallGraph)