blob: 453f4c2fb3ed68f99b051c69b7745c59b35a005d [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Dale Johannesen1f67ce42009-03-19 18:03:56 +000018#include "llvm/IntrinsicInst.h"
Chris Lattner07a38e72003-10-31 21:05:12 +000019#include "llvm/Support/CallSite.h"
Reid Spencerd7d83db2007-02-05 23:42:17 +000020#include "llvm/Support/Compiler.h"
Chris Lattner45cfe542009-08-23 06:03:38 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattnerb81c0212004-04-12 05:36:32 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattner03839952005-12-22 06:07:52 +000024namespace {
25
26//===----------------------------------------------------------------------===//
27// BasicCallGraph class definition
Chris Lattner41fbf302001-09-28 00:08:15 +000028//
Reid Spencerd7d83db2007-02-05 23:42:17 +000029class VISIBILITY_HIDDEN BasicCallGraph : public CallGraph, public ModulePass {
Chris Lattner03839952005-12-22 06:07:52 +000030 // Root is root of the call graph, or the external node if a 'main' function
31 // couldn't be found.
32 //
33 CallGraphNode *Root;
Chris Lattner41fbf302001-09-28 00:08:15 +000034
Chris Lattner03839952005-12-22 06:07:52 +000035 // ExternalCallingNode - This node has edges to all external functions and
36 // those internal functions that have their address taken.
37 CallGraphNode *ExternalCallingNode;
Chris Lattner25e9cad2001-11-26 18:51:25 +000038
Chris Lattner03839952005-12-22 06:07:52 +000039 // CallsExternalNode - This node has edges to it from all functions making
40 // indirect calls or calling an external function.
41 CallGraphNode *CallsExternalNode;
42
43public:
Devang Patel19974732007-05-03 01:11:54 +000044 static char ID; // Class identification, replacement for typeinfo
Dan Gohmanae73dc12008-09-04 17:05:41 +000045 BasicCallGraph() : ModulePass(&ID), Root(0),
Devang Patel794fd752007-05-01 21:15:47 +000046 ExternalCallingNode(0), CallsExternalNode(0) {}
Chris Lattner03839952005-12-22 06:07:52 +000047
48 // runOnModule - Compute the call graph for the specified module.
49 virtual bool runOnModule(Module &M) {
Chris Lattner03839952005-12-22 06:07:52 +000050 CallGraph::initialize(M);
51
Chris Lattnerc54b1c12006-01-14 20:03:00 +000052 ExternalCallingNode = getOrInsertFunction(0);
Chris Lattner03839952005-12-22 06:07:52 +000053 CallsExternalNode = new CallGraphNode(0);
54 Root = 0;
55
56 // Add every function to the call graph...
57 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
58 addToCallGraph(I);
59
60 // If we didn't find a main function, use the external call graph node
61 if (Root == 0) Root = ExternalCallingNode;
62
63 return false;
Chris Lattnerd4d427b2002-03-06 20:19:35 +000064 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000065
Chris Lattner03839952005-12-22 06:07:52 +000066 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
67 AU.setPreservesAll();
68 }
Chris Lattnerd4d427b2002-03-06 20:19:35 +000069
Chris Lattner45cfe542009-08-23 06:03:38 +000070 virtual void print(raw_ostream &OS, const Module *) const {
71 OS << "CallGraph Root is: ";
Chris Lattner03839952005-12-22 06:07:52 +000072 if (Function *F = getRoot()->getFunction())
Chris Lattner45cfe542009-08-23 06:03:38 +000073 OS << F->getName() << "\n";
74 else {
75 OS << "<<null function: 0x" << getRoot() << ">>\n";
76 }
Chris Lattner03839952005-12-22 06:07:52 +000077
Chris Lattner45cfe542009-08-23 06:03:38 +000078 CallGraph::print(OS, 0);
Chris Lattner03839952005-12-22 06:07:52 +000079 }
80
81 virtual void releaseMemory() {
82 destroy();
83 }
84
Chris Lattner03839952005-12-22 06:07:52 +000085 CallGraphNode* getExternalCallingNode() const { return ExternalCallingNode; }
86 CallGraphNode* getCallsExternalNode() const { return CallsExternalNode; }
87
88 // getRoot - Return the root of the call graph, which is either main, or if
89 // main cannot be found, the external node.
Chris Lattnerd4d427b2002-03-06 20:19:35 +000090 //
Chris Lattner03839952005-12-22 06:07:52 +000091 CallGraphNode *getRoot() { return Root; }
92 const CallGraphNode *getRoot() const { return Root; }
93
94private:
95 //===---------------------------------------------------------------------
96 // Implementation of CallGraph construction
97 //
Chris Lattner03839952005-12-22 06:07:52 +000098
Chris Lattner03839952005-12-22 06:07:52 +000099 // addToCallGraph - Add a function to the call graph, and link the node to all
100 // of the functions that it calls.
101 //
102 void addToCallGraph(Function *F) {
Chris Lattnerc54b1c12006-01-14 20:03:00 +0000103 CallGraphNode *Node = getOrInsertFunction(F);
Chris Lattner03839952005-12-22 06:07:52 +0000104
Chris Lattnerd85340f2006-07-12 18:29:36 +0000105 // If this function has external linkage, anything could call it.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000106 if (!F->hasLocalLinkage()) {
Chris Lattnerd85340f2006-07-12 18:29:36 +0000107 ExternalCallingNode->addCalledFunction(CallSite(), Node);
Chris Lattner03839952005-12-22 06:07:52 +0000108
109 // Found the entry point?
110 if (F->getName() == "main") {
111 if (Root) // Found multiple external mains? Don't pick one.
112 Root = ExternalCallingNode;
113 else
114 Root = Node; // Found a main, keep track of it!
115 }
116 }
117
Duncan Sands7ca9d812008-09-09 19:56:34 +0000118 // Loop over all of the users of the function, looking for non-call uses.
119 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I)
Gabor Greifedc4d692009-01-22 21:35:57 +0000120 if ((!isa<CallInst>(I) && !isa<InvokeInst>(I))
121 || !CallSite(cast<Instruction>(I)).isCallee(I)) {
Duncan Sands7ca9d812008-09-09 19:56:34 +0000122 // Not a call, or being used as a parameter rather than as the callee.
123 ExternalCallingNode->addCalledFunction(CallSite(), Node);
124 break;
125 }
126
Chris Lattner03839952005-12-22 06:07:52 +0000127 // If this function is not defined in this translation unit, it could call
128 // anything.
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000129 if (F->isDeclaration() && !F->isIntrinsic())
Chris Lattnerd85340f2006-07-12 18:29:36 +0000130 Node->addCalledFunction(CallSite(), CallsExternalNode);
Chris Lattner03839952005-12-22 06:07:52 +0000131
Duncan Sands99c1a7c2008-09-09 12:40:47 +0000132 // Look for calls by this function.
Chris Lattner03839952005-12-22 06:07:52 +0000133 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
134 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
135 II != IE; ++II) {
Duncan Sands99c1a7c2008-09-09 12:40:47 +0000136 CallSite CS = CallSite::get(II);
Dale Johannesen1f67ce42009-03-19 18:03:56 +0000137 if (CS.getInstruction() && !isa<DbgInfoIntrinsic>(II)) {
Duncan Sands99c1a7c2008-09-09 12:40:47 +0000138 const Function *Callee = CS.getCalledFunction();
139 if (Callee)
140 Node->addCalledFunction(CS, getOrInsertFunction(Callee));
141 else
142 Node->addCalledFunction(CS, CallsExternalNode);
143 }
Chris Lattner03839952005-12-22 06:07:52 +0000144 }
145 }
146
147 //
148 // destroy - Release memory for the call graph
149 virtual void destroy() {
Chris Lattnerf2d9ceb2006-12-05 19:46:12 +0000150 /// CallsExternalNode is not in the function map, delete it explicitly.
Chris Lattner12d38bf2006-12-04 21:22:45 +0000151 delete CallsExternalNode;
152 CallsExternalNode = 0;
Chris Lattnerf2d9ceb2006-12-05 19:46:12 +0000153 CallGraph::destroy();
Chris Lattner03839952005-12-22 06:07:52 +0000154 }
155};
Chris Lattner41fbf302001-09-28 00:08:15 +0000156
Chris Lattner03839952005-12-22 06:07:52 +0000157} //End anonymous namespace
158
Dan Gohman844731a2008-05-13 00:00:25 +0000159static RegisterAnalysisGroup<CallGraph> X("Call Graph");
160static RegisterPass<BasicCallGraph>
161Y("basiccg", "Basic CallGraph Construction", false, true);
162static RegisterAnalysisGroup<CallGraph, true> Z(Y);
163
Devang Patel19974732007-05-03 01:11:54 +0000164char CallGraph::ID = 0;
165char BasicCallGraph::ID = 0;
Lauro Ramos Venancioc7182882007-05-02 20:37:47 +0000166
Chris Lattner03839952005-12-22 06:07:52 +0000167void CallGraph::initialize(Module &M) {
Chris Lattner7e708292002-06-25 16:13:24 +0000168 Mod = &M;
Chris Lattner41fbf302001-09-28 00:08:15 +0000169}
170
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000171void CallGraph::destroy() {
Chris Lattner45d10472006-10-09 17:28:13 +0000172 if (!FunctionMap.empty()) {
Chris Lattner03839952005-12-22 06:07:52 +0000173 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
174 I != E; ++I)
175 delete I->second;
176 FunctionMap.clear();
177 }
Chris Lattner25e9cad2001-11-26 18:51:25 +0000178}
179
Chris Lattner45cfe542009-08-23 06:03:38 +0000180void CallGraph::print(raw_ostream &OS, Module*) const {
Chris Lattner16500152002-11-04 00:21:19 +0000181 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattneraf8a4242004-08-08 03:27:49 +0000182 I->second->print(OS);
183}
Chris Lattner23603a62009-08-30 22:24:32 +0000184void CallGraph::dump() const {
185 print(errs(), 0);
186}
Chris Lattneraf8a4242004-08-08 03:27:49 +0000187
Chris Lattner25e9cad2001-11-26 18:51:25 +0000188//===----------------------------------------------------------------------===//
189// Implementations of public modification methods
190//
191
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000192// removeFunctionFromModule - Unlink the function from this module, returning
193// it. Because this removes the function from the module, the call graph node
194// is destroyed. This is only valid if the function does not call any other
195// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner25e9cad2001-11-26 18:51:25 +0000196// is to dropAllReferences before calling this.
197//
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000198Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
199 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
200 "graph if it references other functions!");
Chris Lattner5714c972003-08-31 20:36:52 +0000201 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattnerd99d4d72002-07-18 04:43:16 +0000202 delete CGN; // Delete the call graph node for this func
Chris Lattner5714c972003-08-31 20:36:52 +0000203 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner25e9cad2001-11-26 18:51:25 +0000204
Chris Lattner5714c972003-08-31 20:36:52 +0000205 Mod->getFunctionList().remove(F);
206 return F;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000207}
208
Chris Lattner6f7e5eb2004-09-18 00:22:13 +0000209// changeFunction - This method changes the function associated with this
210// CallGraphNode, for use by transformations that need to change the prototype
211// of a Function (thus they must create a new Function and move the old code
212// over).
213void CallGraph::changeFunction(Function *OldF, Function *NewF) {
214 iterator I = FunctionMap.find(OldF);
215 CallGraphNode *&New = FunctionMap[NewF];
216 assert(I != FunctionMap.end() && I->second && !New &&
217 "OldF didn't exist in CG or NewF already does!");
218 New = I->second;
Chris Lattner3795bc92004-09-18 00:27:20 +0000219 New->F = NewF;
Chris Lattner6f7e5eb2004-09-18 00:22:13 +0000220 FunctionMap.erase(I);
221}
222
Chris Lattnerc54b1c12006-01-14 20:03:00 +0000223// getOrInsertFunction - This method is identical to calling operator[], but
224// it will insert a new CallGraphNode for the specified function if one does
225// not already exist.
226CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
227 CallGraphNode *&CGN = FunctionMap[F];
228 if (CGN) return CGN;
229
230 assert((!F || F->getParent() == Mod) && "Function not in current module!");
231 return CGN = new CallGraphNode(const_cast<Function*>(F));
232}
233
Chris Lattner45cfe542009-08-23 06:03:38 +0000234void CallGraphNode::print(raw_ostream &OS) const {
Chris Lattner03839952005-12-22 06:07:52 +0000235 if (Function *F = getFunction())
Chris Lattner45cfe542009-08-23 06:03:38 +0000236 OS << "Call graph node for function: '" << F->getName() <<"'\n";
Chris Lattner03839952005-12-22 06:07:52 +0000237 else
238 OS << "Call graph node <<null function: 0x" << this << ">>:\n";
239
Duncan Sands28f02122008-09-08 16:04:03 +0000240 for (const_iterator I = begin(), E = end(); I != E; ++I)
Gabor Greif61046262009-01-14 17:09:04 +0000241 if (Function *FI = I->second->getFunction())
Chris Lattner45cfe542009-08-23 06:03:38 +0000242 OS << " Calls function '" << FI->getName() <<"'\n";
Duncan Sands28f02122008-09-08 16:04:03 +0000243 else
244 OS << " Calls external node\n";
Chris Lattner03839952005-12-22 06:07:52 +0000245 OS << "\n";
246}
247
Chris Lattner45cfe542009-08-23 06:03:38 +0000248void CallGraphNode::dump() const { print(errs()); }
Chris Lattner03839952005-12-22 06:07:52 +0000249
Chris Lattner75caee22008-04-13 19:41:25 +0000250/// removeCallEdgeFor - This method removes the edge in the node for the
251/// specified call site. Note that this method takes linear time, so it
252/// should be used sparingly.
253void CallGraphNode::removeCallEdgeFor(CallSite CS) {
Gabor Greif7b984242009-01-17 00:14:25 +0000254 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
255 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
256 if (I->first == CS) {
257 CalledFunctions.erase(I);
Chris Lattner75caee22008-04-13 19:41:25 +0000258 return;
259 }
260 }
261}
262
263
Chris Lattnercd382a32004-09-18 21:34:34 +0000264// removeAnyCallEdgeTo - This method removes any call edges from this node to
265// the specified callee function. This takes more time to execute than
266// removeCallEdgeTo, so it should not be used unless necessary.
267void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
Chris Lattnerfff03c92004-09-19 19:01:06 +0000268 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
Chris Lattnerd85340f2006-07-12 18:29:36 +0000269 if (CalledFunctions[i].second == Callee) {
Chris Lattnerfff03c92004-09-19 19:01:06 +0000270 CalledFunctions[i] = CalledFunctions.back();
271 CalledFunctions.pop_back();
272 --i; --e;
Chris Lattnercd382a32004-09-18 21:34:34 +0000273 }
274}
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000275
Duncan Sandsa2582da2008-10-03 07:36:09 +0000276/// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite
277/// from this node to the specified callee function.
278void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) {
Gabor Greif7f2e3812009-01-17 19:46:01 +0000279 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
280 assert(I != CalledFunctions.end() && "Cannot find callee to remove!");
281 CallRecord &CR = *I;
Duncan Sandsa2582da2008-10-03 07:36:09 +0000282 if (CR.second == Callee && !CR.first.getInstruction()) {
Gabor Greif7f2e3812009-01-17 19:46:01 +0000283 CalledFunctions.erase(I);
Duncan Sandsa2582da2008-10-03 07:36:09 +0000284 return;
285 }
286 }
287}
288
Duncan Sandsfec2c2b2008-09-06 17:19:29 +0000289/// replaceCallSite - Make the edge in the node for Old CallSite be for
290/// New CallSite instead. Note that this method takes linear time, so it
291/// should be used sparingly.
292void CallGraphNode::replaceCallSite(CallSite Old, CallSite New) {
Gabor Greif7f2e3812009-01-17 19:46:01 +0000293 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
294 assert(I != CalledFunctions.end() && "Cannot find callsite to replace!");
295 if (I->first == Old) {
296 I->first = New;
Duncan Sandsfec2c2b2008-09-06 17:19:29 +0000297 return;
298 }
299 }
300}
301
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000302// Enuse that users of CallGraph.h also link with this file
303DEFINING_FILE_FOR(CallGraph)