blob: e2799d965a7dcd098056b14d0f360bb538fe34d8 [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();
Sanjoy Dasc65d43e2015-06-18 19:28:26 +000082 if (!Callee || !Intrinsic::isLeaf(Callee->getIntrinsicID()))
Rafael Espindola6554e5a2013-10-31 03:03:55 +000083 // Indirect calls of intrinsics are not allowed so no need to check.
Sanjoy Dasc65d43e2015-06-18 19:28:26 +000084 // We can be more precise here by using TargetArg returned by
85 // Intrinsic::isLeaf.
Rafael Espindola6554e5a2013-10-31 03:03:55 +000086 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
Sanjoy Das18c9dd32015-06-19 23:20:31 +0000101 // Print in a deterministic order by sorting CallGraphNodes by name. We do
102 // this here to avoid slowing down the non-printing fast path.
103
104 SmallVector<CallGraphNode *, 16> Nodes;
105 Nodes.reserve(FunctionMap.size());
106
107 for (auto I = begin(), E = end(); I != E; ++I)
108 Nodes.push_back(I->second);
109
110 std::sort(Nodes.begin(), Nodes.end(),
111 [](CallGraphNode *LHS, CallGraphNode *RHS) {
112 if (Function *LF = LHS->getFunction())
113 if (Function *RF = RHS->getFunction())
114 return LF->getName() < RF->getName();
115
116 return RHS->getFunction() != nullptr;
117 });
118
119 for (CallGraphNode *CN : Nodes)
120 CN->print(OS);
Chris Lattner6b5411022004-08-08 03:27:49 +0000121}
122
Chandler Carruth6378cf52013-11-26 04:19:30 +0000123#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
124void CallGraph::dump() const { print(dbgs()); }
125#endif
Chris Lattner03946cd2001-11-26 18:51:25 +0000126
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000127// removeFunctionFromModule - Unlink the function from this module, returning
128// it. Because this removes the function from the module, the call graph node
129// is destroyed. This is only valid if the function does not call any other
130// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner03946cd2001-11-26 18:51:25 +0000131// is to dropAllReferences before calling this.
132//
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000133Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
Chris Lattner4275cf22009-08-31 02:24:20 +0000134 assert(CGN->empty() && "Cannot remove function from call "
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000135 "graph if it references other functions!");
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000136 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000137 delete CGN; // Delete the call graph node for this func
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000138 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner03946cd2001-11-26 18:51:25 +0000139
Chandler Carruth6378cf52013-11-26 04:19:30 +0000140 M.getFunctionList().remove(F);
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000141 return F;
Chris Lattner03946cd2001-11-26 18:51:25 +0000142}
143
Nick Lewycky0f87ca72011-01-03 03:19:35 +0000144/// spliceFunction - Replace the function represented by this node by another.
145/// This does not rescan the body of the function, so it is suitable when
146/// splicing the body of the old function to the new while also updating all
147/// callers from old to new.
148///
149void CallGraph::spliceFunction(const Function *From, const Function *To) {
150 assert(FunctionMap.count(From) && "No CallGraphNode for function!");
151 assert(!FunctionMap.count(To) &&
152 "Pointing CallGraphNode at a function that already exists");
153 FunctionMapTy::iterator I = FunctionMap.find(From);
154 I->second->F = const_cast<Function*>(To);
155 FunctionMap[To] = I->second;
156 FunctionMap.erase(I);
157}
158
Chris Lattner00ca8d22006-01-14 20:03:00 +0000159// getOrInsertFunction - This method is identical to calling operator[], but
160// it will insert a new CallGraphNode for the specified function if one does
161// not already exist.
162CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
163 CallGraphNode *&CGN = FunctionMap[F];
Chandler Carruth6378cf52013-11-26 04:19:30 +0000164 if (CGN)
165 return CGN;
166
167 assert((!F || F->getParent() == &M) && "Function not in current module!");
Chris Lattner00ca8d22006-01-14 20:03:00 +0000168 return CGN = new CallGraphNode(const_cast<Function*>(F));
169}
170
Chandler Carruth6378cf52013-11-26 04:19:30 +0000171//===----------------------------------------------------------------------===//
172// Implementations of the CallGraphNode class methods.
173//
174
Chris Lattner13626022009-08-23 06:03:38 +0000175void CallGraphNode::print(raw_ostream &OS) const {
Chris Lattnerbe1987772005-12-22 06:07:52 +0000176 if (Function *F = getFunction())
Chris Lattner081375b2009-08-31 03:15:49 +0000177 OS << "Call graph node for function: '" << F->getName() << "'";
Chris Lattnerbe1987772005-12-22 06:07:52 +0000178 else
Chris Lattner081375b2009-08-31 03:15:49 +0000179 OS << "Call graph node <<null function>>";
180
Chris Lattner8c562542010-04-23 18:23:40 +0000181 OS << "<<" << this << ">> #uses=" << getNumReferences() << '\n';
Chris Lattnerbe1987772005-12-22 06:07:52 +0000182
Chris Lattner8c562542010-04-23 18:23:40 +0000183 for (const_iterator I = begin(), E = end(); I != E; ++I) {
184 OS << " CS<" << I->first << "> calls ";
Gabor Greif191812f2009-01-14 17:09:04 +0000185 if (Function *FI = I->second->getFunction())
Chris Lattner8c562542010-04-23 18:23:40 +0000186 OS << "function '" << FI->getName() <<"'\n";
187 else
188 OS << "external node\n";
189 }
190 OS << '\n';
Chris Lattnerbe1987772005-12-22 06:07:52 +0000191}
192
Manman Ren49d684e2012-09-12 05:06:18 +0000193#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
David Greeneba44b3e2009-12-23 20:03:58 +0000194void CallGraphNode::dump() const { print(dbgs()); }
Manman Renc3366cc2012-09-06 19:55:56 +0000195#endif
Chris Lattnerbe1987772005-12-22 06:07:52 +0000196
Chris Lattnercc9709c2008-04-13 19:41:25 +0000197/// removeCallEdgeFor - This method removes the edge in the node for the
198/// specified call site. Note that this method takes linear time, so it
199/// should be used sparingly.
200void CallGraphNode::removeCallEdgeFor(CallSite CS) {
Chris Lattnereedcd842009-08-31 07:23:46 +0000201 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
202 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
Chris Lattner063d0652009-09-01 06:31:31 +0000203 if (I->first == CS.getInstruction()) {
Chris Lattnereedcd842009-08-31 07:23:46 +0000204 I->second->DropRef();
205 *I = CalledFunctions.back();
206 CalledFunctions.pop_back();
207 return;
208 }
Chris Lattnercc9709c2008-04-13 19:41:25 +0000209 }
210}
211
Chris Lattner824a2182004-09-18 21:34:34 +0000212// removeAnyCallEdgeTo - This method removes any call edges from this node to
213// the specified callee function. This takes more time to execute than
214// removeCallEdgeTo, so it should not be used unless necessary.
215void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
Chris Lattnerd6d99df2004-09-19 19:01:06 +0000216 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000217 if (CalledFunctions[i].second == Callee) {
Chris Lattner081375b2009-08-31 03:15:49 +0000218 Callee->DropRef();
Chris Lattnerd6d99df2004-09-19 19:01:06 +0000219 CalledFunctions[i] = CalledFunctions.back();
220 CalledFunctions.pop_back();
221 --i; --e;
Chris Lattner824a2182004-09-18 21:34:34 +0000222 }
223}
Reid Spencerbe535662006-06-07 22:00:26 +0000224
Duncan Sands3a813a52008-10-03 07:36:09 +0000225/// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite
226/// from this node to the specified callee function.
227void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) {
Gabor Greif20b722f2009-01-17 19:46:01 +0000228 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
229 assert(I != CalledFunctions.end() && "Cannot find callee to remove!");
230 CallRecord &CR = *I;
Craig Topper353eda42014-04-24 06:44:33 +0000231 if (CR.second == Callee && CR.first == nullptr) {
Chris Lattner081375b2009-08-31 03:15:49 +0000232 Callee->DropRef();
233 *I = CalledFunctions.back();
234 CalledFunctions.pop_back();
Duncan Sands3a813a52008-10-03 07:36:09 +0000235 return;
236 }
237 }
238}
239
Chris Lattnere0987212009-09-15 05:40:35 +0000240/// replaceCallEdge - This method replaces the edge in the node for the
241/// specified call site with a new one. Note that this method takes linear
242/// time, so it should be used sparingly.
243void CallGraphNode::replaceCallEdge(CallSite CS,
244 CallSite NewCS, CallGraphNode *NewNode){
245 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
246 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
247 if (I->first == CS.getInstruction()) {
248 I->second->DropRef();
249 I->first = NewCS.getInstruction();
250 I->second = NewNode;
251 NewNode->AddRef();
252 return;
253 }
254 }
255}
256
Chandler Carruth6378cf52013-11-26 04:19:30 +0000257//===----------------------------------------------------------------------===//
Chandler Carruthc4ddab62014-01-05 10:38:52 +0000258// Out-of-line definitions of CallGraphAnalysis class members.
259//
260
261char CallGraphAnalysis::PassID;
262
263//===----------------------------------------------------------------------===//
Chandler Carruth6378cf52013-11-26 04:19:30 +0000264// Implementations of the CallGraphWrapperPass class methods.
265//
266
267CallGraphWrapperPass::CallGraphWrapperPass() : ModulePass(ID) {
268 initializeCallGraphWrapperPassPass(*PassRegistry::getPassRegistry());
269}
270
271CallGraphWrapperPass::~CallGraphWrapperPass() {}
272
273void CallGraphWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
274 AU.setPreservesAll();
275}
276
277bool CallGraphWrapperPass::runOnModule(Module &M) {
278 // All the real work is done in the constructor for the CallGraph.
279 G.reset(new CallGraph(M));
280 return false;
281}
282
283INITIALIZE_PASS(CallGraphWrapperPass, "basiccg", "CallGraph Construction",
284 false, true)
285
286char CallGraphWrapperPass::ID = 0;
287
David Blaikieb61064e2014-07-19 01:05:11 +0000288void CallGraphWrapperPass::releaseMemory() { G.reset(); }
Chandler Carruth6378cf52013-11-26 04:19:30 +0000289
290void CallGraphWrapperPass::print(raw_ostream &OS, const Module *) const {
291 if (!G) {
292 OS << "No call graph has been built!\n";
293 return;
294 }
295
296 // Just delegate.
297 G->print(OS);
298}
299
300#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Craig Toppere73658d2014-04-28 04:05:08 +0000301void CallGraphWrapperPass::dump() const { print(dbgs(), nullptr); }
Chandler Carruth6378cf52013-11-26 04:19:30 +0000302#endif