blob: ee2763939647d6a53e62c221bf04c6ed965b8f9e [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.
Yaron Keren26ceb082015-06-12 05:15:27 +000027 for (Function &F : M)
28 addToCallGraph(&F);
Chandler Carruth6378cf52013-11-26 04:19:30 +000029
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
Yaron Keren26ceb082015-06-12 05:15:27 +000043 for (auto &I : FunctionMap)
44 I.second->allReferencesDropped();
Chandler Carruth6378cf52013-11-26 04:19:30 +000045#endif
Yaron Keren26ceb082015-06-12 05:15:27 +000046 for (auto &I : FunctionMap)
47 delete I.second;
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000048}
49
Rafael Espindola6554e5a2013-10-31 03:03:55 +000050void CallGraph::addToCallGraph(Function *F) {
51 CallGraphNode *Node = getOrInsertFunction(F);
52
53 // If this function has external linkage, anything could call it.
54 if (!F->hasLocalLinkage()) {
55 ExternalCallingNode->addCalledFunction(CallSite(), Node);
56
57 // Found the entry point?
58 if (F->getName() == "main") {
59 if (Root) // Found multiple external mains? Don't pick one.
60 Root = ExternalCallingNode;
61 else
62 Root = Node; // Found a main, keep track of it!
63 }
64 }
65
66 // If this function has its address taken, anything could call it.
67 if (F->hasAddressTaken())
68 ExternalCallingNode->addCalledFunction(CallSite(), Node);
69
70 // If this function is not defined in this translation unit, it could call
71 // anything.
72 if (F->isDeclaration() && !F->isIntrinsic())
73 Node->addCalledFunction(CallSite(), CallsExternalNode);
74
75 // Look for calls by this function.
76 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
77 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE;
78 ++II) {
79 CallSite CS(cast<Value>(II));
80 if (CS) {
81 const Function *Callee = CS.getCalledFunction();
82 if (!Callee)
83 // Indirect calls of intrinsics are not allowed so no need to check.
84 Node->addCalledFunction(CS, CallsExternalNode);
85 else if (!Callee->isIntrinsic())
86 Node->addCalledFunction(CS, getOrInsertFunction(Callee));
87 }
88 }
89}
90
Chandler Carruth6378cf52013-11-26 04:19:30 +000091void CallGraph::print(raw_ostream &OS) const {
Rafael Espindola6554e5a2013-10-31 03:03:55 +000092 OS << "CallGraph Root is: ";
93 if (Function *F = Root->getFunction())
94 OS << F->getName() << "\n";
95 else {
96 OS << "<<null function: 0x" << Root << ">>\n";
97 }
98
Chris Lattnerb9d55472002-11-04 00:21:19 +000099 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner6b5411022004-08-08 03:27:49 +0000100 I->second->print(OS);
101}
102
Chandler Carruth6378cf52013-11-26 04:19:30 +0000103#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
104void CallGraph::dump() const { print(dbgs()); }
105#endif
Chris Lattner03946cd2001-11-26 18:51:25 +0000106
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000107// removeFunctionFromModule - Unlink the function from this module, returning
108// it. Because this removes the function from the module, the call graph node
109// is destroyed. This is only valid if the function does not call any other
110// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner03946cd2001-11-26 18:51:25 +0000111// is to dropAllReferences before calling this.
112//
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000113Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
Chris Lattner4275cf22009-08-31 02:24:20 +0000114 assert(CGN->empty() && "Cannot remove function from call "
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000115 "graph if it references other functions!");
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000116 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000117 delete CGN; // Delete the call graph node for this func
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000118 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner03946cd2001-11-26 18:51:25 +0000119
Chandler Carruth6378cf52013-11-26 04:19:30 +0000120 M.getFunctionList().remove(F);
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000121 return F;
Chris Lattner03946cd2001-11-26 18:51:25 +0000122}
123
Nick Lewycky0f87ca72011-01-03 03:19:35 +0000124/// spliceFunction - Replace the function represented by this node by another.
125/// This does not rescan the body of the function, so it is suitable when
126/// splicing the body of the old function to the new while also updating all
127/// callers from old to new.
128///
129void CallGraph::spliceFunction(const Function *From, const Function *To) {
130 assert(FunctionMap.count(From) && "No CallGraphNode for function!");
131 assert(!FunctionMap.count(To) &&
132 "Pointing CallGraphNode at a function that already exists");
133 FunctionMapTy::iterator I = FunctionMap.find(From);
134 I->second->F = const_cast<Function*>(To);
135 FunctionMap[To] = I->second;
136 FunctionMap.erase(I);
137}
138
Chris Lattner00ca8d22006-01-14 20:03:00 +0000139// getOrInsertFunction - This method is identical to calling operator[], but
140// it will insert a new CallGraphNode for the specified function if one does
141// not already exist.
142CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
143 CallGraphNode *&CGN = FunctionMap[F];
Chandler Carruth6378cf52013-11-26 04:19:30 +0000144 if (CGN)
145 return CGN;
146
147 assert((!F || F->getParent() == &M) && "Function not in current module!");
Chris Lattner00ca8d22006-01-14 20:03:00 +0000148 return CGN = new CallGraphNode(const_cast<Function*>(F));
149}
150
Chandler Carruth6378cf52013-11-26 04:19:30 +0000151//===----------------------------------------------------------------------===//
152// Implementations of the CallGraphNode class methods.
153//
154
Chris Lattner13626022009-08-23 06:03:38 +0000155void CallGraphNode::print(raw_ostream &OS) const {
Chris Lattnerbe1987772005-12-22 06:07:52 +0000156 if (Function *F = getFunction())
Chris Lattner081375b2009-08-31 03:15:49 +0000157 OS << "Call graph node for function: '" << F->getName() << "'";
Chris Lattnerbe1987772005-12-22 06:07:52 +0000158 else
Chris Lattner081375b2009-08-31 03:15:49 +0000159 OS << "Call graph node <<null function>>";
160
Chris Lattner8c562542010-04-23 18:23:40 +0000161 OS << "<<" << this << ">> #uses=" << getNumReferences() << '\n';
Chris Lattnerbe1987772005-12-22 06:07:52 +0000162
Chris Lattner8c562542010-04-23 18:23:40 +0000163 for (const_iterator I = begin(), E = end(); I != E; ++I) {
164 OS << " CS<" << I->first << "> calls ";
Gabor Greif191812f2009-01-14 17:09:04 +0000165 if (Function *FI = I->second->getFunction())
Chris Lattner8c562542010-04-23 18:23:40 +0000166 OS << "function '" << FI->getName() <<"'\n";
167 else
168 OS << "external node\n";
169 }
170 OS << '\n';
Chris Lattnerbe1987772005-12-22 06:07:52 +0000171}
172
Manman Ren49d684e2012-09-12 05:06:18 +0000173#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
David Greeneba44b3e2009-12-23 20:03:58 +0000174void CallGraphNode::dump() const { print(dbgs()); }
Manman Renc3366cc2012-09-06 19:55:56 +0000175#endif
Chris Lattnerbe1987772005-12-22 06:07:52 +0000176
Chris Lattnercc9709c2008-04-13 19:41:25 +0000177/// removeCallEdgeFor - This method removes the edge in the node for the
178/// specified call site. Note that this method takes linear time, so it
179/// should be used sparingly.
180void CallGraphNode::removeCallEdgeFor(CallSite CS) {
Chris Lattnereedcd842009-08-31 07:23:46 +0000181 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
182 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
Chris Lattner063d0652009-09-01 06:31:31 +0000183 if (I->first == CS.getInstruction()) {
Chris Lattnereedcd842009-08-31 07:23:46 +0000184 I->second->DropRef();
185 *I = CalledFunctions.back();
186 CalledFunctions.pop_back();
187 return;
188 }
Chris Lattnercc9709c2008-04-13 19:41:25 +0000189 }
190}
191
Chris Lattner824a2182004-09-18 21:34:34 +0000192// removeAnyCallEdgeTo - This method removes any call edges from this node to
193// the specified callee function. This takes more time to execute than
194// removeCallEdgeTo, so it should not be used unless necessary.
195void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
Chris Lattnerd6d99df2004-09-19 19:01:06 +0000196 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000197 if (CalledFunctions[i].second == Callee) {
Chris Lattner081375b2009-08-31 03:15:49 +0000198 Callee->DropRef();
Chris Lattnerd6d99df2004-09-19 19:01:06 +0000199 CalledFunctions[i] = CalledFunctions.back();
200 CalledFunctions.pop_back();
201 --i; --e;
Chris Lattner824a2182004-09-18 21:34:34 +0000202 }
203}
Reid Spencerbe535662006-06-07 22:00:26 +0000204
Duncan Sands3a813a52008-10-03 07:36:09 +0000205/// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite
206/// from this node to the specified callee function.
207void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) {
Gabor Greif20b722f2009-01-17 19:46:01 +0000208 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
209 assert(I != CalledFunctions.end() && "Cannot find callee to remove!");
210 CallRecord &CR = *I;
Craig Topper353eda42014-04-24 06:44:33 +0000211 if (CR.second == Callee && CR.first == nullptr) {
Chris Lattner081375b2009-08-31 03:15:49 +0000212 Callee->DropRef();
213 *I = CalledFunctions.back();
214 CalledFunctions.pop_back();
Duncan Sands3a813a52008-10-03 07:36:09 +0000215 return;
216 }
217 }
218}
219
Chris Lattnere0987212009-09-15 05:40:35 +0000220/// replaceCallEdge - This method replaces the edge in the node for the
221/// specified call site with a new one. Note that this method takes linear
222/// time, so it should be used sparingly.
223void CallGraphNode::replaceCallEdge(CallSite CS,
224 CallSite NewCS, CallGraphNode *NewNode){
225 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
226 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
227 if (I->first == CS.getInstruction()) {
228 I->second->DropRef();
229 I->first = NewCS.getInstruction();
230 I->second = NewNode;
231 NewNode->AddRef();
232 return;
233 }
234 }
235}
236
Chandler Carruth6378cf52013-11-26 04:19:30 +0000237//===----------------------------------------------------------------------===//
Chandler Carruthc4ddab62014-01-05 10:38:52 +0000238// Out-of-line definitions of CallGraphAnalysis class members.
239//
240
241char CallGraphAnalysis::PassID;
242
243//===----------------------------------------------------------------------===//
Chandler Carruth6378cf52013-11-26 04:19:30 +0000244// Implementations of the CallGraphWrapperPass class methods.
245//
246
247CallGraphWrapperPass::CallGraphWrapperPass() : ModulePass(ID) {
248 initializeCallGraphWrapperPassPass(*PassRegistry::getPassRegistry());
249}
250
251CallGraphWrapperPass::~CallGraphWrapperPass() {}
252
253void CallGraphWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
254 AU.setPreservesAll();
255}
256
257bool CallGraphWrapperPass::runOnModule(Module &M) {
258 // All the real work is done in the constructor for the CallGraph.
259 G.reset(new CallGraph(M));
260 return false;
261}
262
263INITIALIZE_PASS(CallGraphWrapperPass, "basiccg", "CallGraph Construction",
264 false, true)
265
266char CallGraphWrapperPass::ID = 0;
267
David Blaikieb61064e2014-07-19 01:05:11 +0000268void CallGraphWrapperPass::releaseMemory() { G.reset(); }
Chandler Carruth6378cf52013-11-26 04:19:30 +0000269
270void CallGraphWrapperPass::print(raw_ostream &OS, const Module *) const {
271 if (!G) {
272 OS << "No call graph has been built!\n";
273 return;
274 }
275
276 // Just delegate.
277 G->print(OS);
278}
279
280#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Craig Toppere73658d2014-04-28 04:05:08 +0000281void CallGraphWrapperPass::dump() const { print(dbgs(), nullptr); }
Chandler Carruth6378cf52013-11-26 04:19:30 +0000282#endif