blob: caec2534691afa4d7e69f773419e5365e92cedf9 [file] [log] [blame]
Chris Lattnerd852cc32002-03-06 18:00:49 +00001//===- CallGraph.cpp - Build a Module's call graph ------------------------===//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman01808ca2005-04-21 21:13:18 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerbbf3ae82001-09-28 00:08:15 +00009
10#include "llvm/Analysis/CallGraph.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000011#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000012#include "llvm/IR/Instructions.h"
13#include "llvm/IR/IntrinsicInst.h"
14#include "llvm/IR/Module.h"
David Greeneba44b3e2009-12-23 20:03:58 +000015#include "llvm/Support/Debug.h"
Chris Lattner13626022009-08-23 06:03:38 +000016#include "llvm/Support/raw_ostream.h"
Chris Lattner8b6db182004-04-12 05:36:32 +000017using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000018
Chandler Carruth6378cf52013-11-26 04:19:30 +000019//===----------------------------------------------------------------------===//
20// Implementations of the CallGraph class methods.
21//
22
23CallGraph::CallGraph(Module &M)
Craig Topper353eda42014-04-24 06:44:33 +000024 : M(M), Root(nullptr), ExternalCallingNode(getOrInsertFunction(nullptr)),
25 CallsExternalNode(new CallGraphNode(nullptr)) {
Chandler Carruth6378cf52013-11-26 04:19:30 +000026 // Add every function to the call graph.
27 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
28 addToCallGraph(I);
29
30 // If we didn't find a main function, use the external call graph node
Craig Topper353eda42014-04-24 06:44:33 +000031 if (!Root)
Chandler Carruth6378cf52013-11-26 04:19:30 +000032 Root = ExternalCallingNode;
33}
34
35CallGraph::~CallGraph() {
36 // CallsExternalNode is not in the function map, delete it explicitly.
37 CallsExternalNode->allReferencesDropped();
38 delete CallsExternalNode;
39
40// Reset all node's use counts to zero before deleting them to prevent an
41// assertion from firing.
42#ifndef NDEBUG
43 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
44 I != E; ++I)
45 I->second->allReferencesDropped();
46#endif
47 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
48 I != E; ++I)
49 delete I->second;
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000050}
51
Rafael Espindola6554e5a2013-10-31 03:03:55 +000052void CallGraph::addToCallGraph(Function *F) {
53 CallGraphNode *Node = getOrInsertFunction(F);
54
55 // If this function has external linkage, anything could call it.
56 if (!F->hasLocalLinkage()) {
57 ExternalCallingNode->addCalledFunction(CallSite(), Node);
58
59 // Found the entry point?
60 if (F->getName() == "main") {
61 if (Root) // Found multiple external mains? Don't pick one.
62 Root = ExternalCallingNode;
63 else
64 Root = Node; // Found a main, keep track of it!
65 }
66 }
67
68 // If this function has its address taken, anything could call it.
69 if (F->hasAddressTaken())
70 ExternalCallingNode->addCalledFunction(CallSite(), Node);
71
72 // If this function is not defined in this translation unit, it could call
73 // anything.
74 if (F->isDeclaration() && !F->isIntrinsic())
75 Node->addCalledFunction(CallSite(), CallsExternalNode);
76
77 // Look for calls by this function.
78 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
79 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE;
80 ++II) {
81 CallSite CS(cast<Value>(II));
82 if (CS) {
83 const Function *Callee = CS.getCalledFunction();
84 if (!Callee)
85 // Indirect calls of intrinsics are not allowed so no need to check.
86 Node->addCalledFunction(CS, CallsExternalNode);
87 else if (!Callee->isIntrinsic())
88 Node->addCalledFunction(CS, getOrInsertFunction(Callee));
89 }
90 }
91}
92
Chandler Carruth6378cf52013-11-26 04:19:30 +000093void CallGraph::print(raw_ostream &OS) const {
Rafael Espindola6554e5a2013-10-31 03:03:55 +000094 OS << "CallGraph Root is: ";
95 if (Function *F = Root->getFunction())
96 OS << F->getName() << "\n";
97 else {
98 OS << "<<null function: 0x" << Root << ">>\n";
99 }
100
Chris Lattnerb9d55472002-11-04 00:21:19 +0000101 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner6b5411022004-08-08 03:27:49 +0000102 I->second->print(OS);
103}
104
Chandler Carruth6378cf52013-11-26 04:19:30 +0000105#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
106void CallGraph::dump() const { print(dbgs()); }
107#endif
Chris Lattner03946cd2001-11-26 18:51:25 +0000108
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000109// removeFunctionFromModule - Unlink the function from this module, returning
110// it. Because this removes the function from the module, the call graph node
111// is destroyed. This is only valid if the function does not call any other
112// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner03946cd2001-11-26 18:51:25 +0000113// is to dropAllReferences before calling this.
114//
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000115Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
Chris Lattner4275cf22009-08-31 02:24:20 +0000116 assert(CGN->empty() && "Cannot remove function from call "
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000117 "graph if it references other functions!");
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000118 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000119 delete CGN; // Delete the call graph node for this func
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000120 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner03946cd2001-11-26 18:51:25 +0000121
Chandler Carruth6378cf52013-11-26 04:19:30 +0000122 M.getFunctionList().remove(F);
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000123 return F;
Chris Lattner03946cd2001-11-26 18:51:25 +0000124}
125
Nick Lewycky0f87ca72011-01-03 03:19:35 +0000126/// spliceFunction - Replace the function represented by this node by another.
127/// This does not rescan the body of the function, so it is suitable when
128/// splicing the body of the old function to the new while also updating all
129/// callers from old to new.
130///
131void CallGraph::spliceFunction(const Function *From, const Function *To) {
132 assert(FunctionMap.count(From) && "No CallGraphNode for function!");
133 assert(!FunctionMap.count(To) &&
134 "Pointing CallGraphNode at a function that already exists");
135 FunctionMapTy::iterator I = FunctionMap.find(From);
136 I->second->F = const_cast<Function*>(To);
137 FunctionMap[To] = I->second;
138 FunctionMap.erase(I);
139}
140
Chris Lattner00ca8d22006-01-14 20:03:00 +0000141// getOrInsertFunction - This method is identical to calling operator[], but
142// it will insert a new CallGraphNode for the specified function if one does
143// not already exist.
144CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
145 CallGraphNode *&CGN = FunctionMap[F];
Chandler Carruth6378cf52013-11-26 04:19:30 +0000146 if (CGN)
147 return CGN;
148
149 assert((!F || F->getParent() == &M) && "Function not in current module!");
Chris Lattner00ca8d22006-01-14 20:03:00 +0000150 return CGN = new CallGraphNode(const_cast<Function*>(F));
151}
152
Chandler Carruth6378cf52013-11-26 04:19:30 +0000153//===----------------------------------------------------------------------===//
154// Implementations of the CallGraphNode class methods.
155//
156
Chris Lattner13626022009-08-23 06:03:38 +0000157void CallGraphNode::print(raw_ostream &OS) const {
Chris Lattnerbe1987772005-12-22 06:07:52 +0000158 if (Function *F = getFunction())
Chris Lattner081375b2009-08-31 03:15:49 +0000159 OS << "Call graph node for function: '" << F->getName() << "'";
Chris Lattnerbe1987772005-12-22 06:07:52 +0000160 else
Chris Lattner081375b2009-08-31 03:15:49 +0000161 OS << "Call graph node <<null function>>";
162
Chris Lattner8c562542010-04-23 18:23:40 +0000163 OS << "<<" << this << ">> #uses=" << getNumReferences() << '\n';
Chris Lattnerbe1987772005-12-22 06:07:52 +0000164
Chris Lattner8c562542010-04-23 18:23:40 +0000165 for (const_iterator I = begin(), E = end(); I != E; ++I) {
166 OS << " CS<" << I->first << "> calls ";
Gabor Greif191812f2009-01-14 17:09:04 +0000167 if (Function *FI = I->second->getFunction())
Chris Lattner8c562542010-04-23 18:23:40 +0000168 OS << "function '" << FI->getName() <<"'\n";
169 else
170 OS << "external node\n";
171 }
172 OS << '\n';
Chris Lattnerbe1987772005-12-22 06:07:52 +0000173}
174
Manman Ren49d684e2012-09-12 05:06:18 +0000175#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
David Greeneba44b3e2009-12-23 20:03:58 +0000176void CallGraphNode::dump() const { print(dbgs()); }
Manman Renc3366cc2012-09-06 19:55:56 +0000177#endif
Chris Lattnerbe1987772005-12-22 06:07:52 +0000178
Chris Lattnercc9709c2008-04-13 19:41:25 +0000179/// removeCallEdgeFor - This method removes the edge in the node for the
180/// specified call site. Note that this method takes linear time, so it
181/// should be used sparingly.
182void CallGraphNode::removeCallEdgeFor(CallSite CS) {
Chris Lattnereedcd842009-08-31 07:23:46 +0000183 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
184 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
Chris Lattner063d0652009-09-01 06:31:31 +0000185 if (I->first == CS.getInstruction()) {
Chris Lattnereedcd842009-08-31 07:23:46 +0000186 I->second->DropRef();
187 *I = CalledFunctions.back();
188 CalledFunctions.pop_back();
189 return;
190 }
Chris Lattnercc9709c2008-04-13 19:41:25 +0000191 }
192}
193
Chris Lattner824a2182004-09-18 21:34:34 +0000194// removeAnyCallEdgeTo - This method removes any call edges from this node to
195// the specified callee function. This takes more time to execute than
196// removeCallEdgeTo, so it should not be used unless necessary.
197void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
Chris Lattnerd6d99df2004-09-19 19:01:06 +0000198 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000199 if (CalledFunctions[i].second == Callee) {
Chris Lattner081375b2009-08-31 03:15:49 +0000200 Callee->DropRef();
Chris Lattnerd6d99df2004-09-19 19:01:06 +0000201 CalledFunctions[i] = CalledFunctions.back();
202 CalledFunctions.pop_back();
203 --i; --e;
Chris Lattner824a2182004-09-18 21:34:34 +0000204 }
205}
Reid Spencerbe535662006-06-07 22:00:26 +0000206
Duncan Sands3a813a52008-10-03 07:36:09 +0000207/// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite
208/// from this node to the specified callee function.
209void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) {
Gabor Greif20b722f2009-01-17 19:46:01 +0000210 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
211 assert(I != CalledFunctions.end() && "Cannot find callee to remove!");
212 CallRecord &CR = *I;
Craig Topper353eda42014-04-24 06:44:33 +0000213 if (CR.second == Callee && CR.first == nullptr) {
Chris Lattner081375b2009-08-31 03:15:49 +0000214 Callee->DropRef();
215 *I = CalledFunctions.back();
216 CalledFunctions.pop_back();
Duncan Sands3a813a52008-10-03 07:36:09 +0000217 return;
218 }
219 }
220}
221
Chris Lattnere0987212009-09-15 05:40:35 +0000222/// replaceCallEdge - This method replaces the edge in the node for the
223/// specified call site with a new one. Note that this method takes linear
224/// time, so it should be used sparingly.
225void CallGraphNode::replaceCallEdge(CallSite CS,
226 CallSite NewCS, CallGraphNode *NewNode){
227 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
228 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
229 if (I->first == CS.getInstruction()) {
230 I->second->DropRef();
231 I->first = NewCS.getInstruction();
232 I->second = NewNode;
233 NewNode->AddRef();
234 return;
235 }
236 }
237}
238
Chandler Carruth6378cf52013-11-26 04:19:30 +0000239//===----------------------------------------------------------------------===//
Chandler Carruthc4ddab62014-01-05 10:38:52 +0000240// Out-of-line definitions of CallGraphAnalysis class members.
241//
242
243char CallGraphAnalysis::PassID;
244
245//===----------------------------------------------------------------------===//
Chandler Carruth6378cf52013-11-26 04:19:30 +0000246// Implementations of the CallGraphWrapperPass class methods.
247//
248
249CallGraphWrapperPass::CallGraphWrapperPass() : ModulePass(ID) {
250 initializeCallGraphWrapperPassPass(*PassRegistry::getPassRegistry());
251}
252
253CallGraphWrapperPass::~CallGraphWrapperPass() {}
254
255void CallGraphWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
256 AU.setPreservesAll();
257}
258
259bool CallGraphWrapperPass::runOnModule(Module &M) {
260 // All the real work is done in the constructor for the CallGraph.
261 G.reset(new CallGraph(M));
262 return false;
263}
264
265INITIALIZE_PASS(CallGraphWrapperPass, "basiccg", "CallGraph Construction",
266 false, true)
267
268char CallGraphWrapperPass::ID = 0;
269
Craig Topper353eda42014-04-24 06:44:33 +0000270void CallGraphWrapperPass::releaseMemory() { G.reset(nullptr); }
Chandler Carruth6378cf52013-11-26 04:19:30 +0000271
272void CallGraphWrapperPass::print(raw_ostream &OS, const Module *) const {
273 if (!G) {
274 OS << "No call graph has been built!\n";
275 return;
276 }
277
278 // Just delegate.
279 G->print(OS);
280}
281
282#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Craig Toppere73658d2014-04-28 04:05:08 +0000283void CallGraphWrapperPass::dump() const { print(dbgs(), nullptr); }
Chandler Carruth6378cf52013-11-26 04:19:30 +0000284#endif
285
Reid Spencerbe535662006-06-07 22:00:26 +0000286// Enuse that users of CallGraph.h also link with this file
287DEFINING_FILE_FOR(CallGraph)