blob: f042964c21d918eebc66c2d41dfd2ca2b0dd9593 [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 Carruth9fb823b2013-01-02 11:36:10 +000011#include "llvm/IR/Instructions.h"
12#include "llvm/IR/IntrinsicInst.h"
13#include "llvm/IR/Module.h"
Chris Lattner9c4a58b2003-10-31 21:05:12 +000014#include "llvm/Support/CallSite.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
Rafael Espindola6554e5a2013-10-31 03:03:55 +000019CallGraph::CallGraph()
20 : ModulePass(ID), Root(0), ExternalCallingNode(0), CallsExternalNode(0) {
21 initializeCallGraphPass(*PassRegistry::getPassRegistry());
Chris Lattnerbbf3ae82001-09-28 00:08:15 +000022}
23
Rafael Espindola6554e5a2013-10-31 03:03:55 +000024void CallGraph::addToCallGraph(Function *F) {
25 CallGraphNode *Node = getOrInsertFunction(F);
26
27 // If this function has external linkage, anything could call it.
28 if (!F->hasLocalLinkage()) {
29 ExternalCallingNode->addCalledFunction(CallSite(), Node);
30
31 // Found the entry point?
32 if (F->getName() == "main") {
33 if (Root) // Found multiple external mains? Don't pick one.
34 Root = ExternalCallingNode;
35 else
36 Root = Node; // Found a main, keep track of it!
37 }
38 }
39
40 // If this function has its address taken, anything could call it.
41 if (F->hasAddressTaken())
42 ExternalCallingNode->addCalledFunction(CallSite(), Node);
43
44 // If this function is not defined in this translation unit, it could call
45 // anything.
46 if (F->isDeclaration() && !F->isIntrinsic())
47 Node->addCalledFunction(CallSite(), CallsExternalNode);
48
49 // Look for calls by this function.
50 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
51 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE;
52 ++II) {
53 CallSite CS(cast<Value>(II));
54 if (CS) {
55 const Function *Callee = CS.getCalledFunction();
56 if (!Callee)
57 // Indirect calls of intrinsics are not allowed so no need to check.
58 Node->addCalledFunction(CS, CallsExternalNode);
59 else if (!Callee->isIntrinsic())
60 Node->addCalledFunction(CS, getOrInsertFunction(Callee));
61 }
62 }
63}
64
65void CallGraph::getAnalysisUsage(AnalysisUsage &AU) const {
66 AU.setPreservesAll();
67}
68
69bool CallGraph::runOnModule(Module &M) {
70 Mod = &M;
71
72 ExternalCallingNode = getOrInsertFunction(0);
73 assert(!CallsExternalNode);
74 CallsExternalNode = new CallGraphNode(0);
75 Root = 0;
76
77 // Add every function to the call graph.
78 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
79 addToCallGraph(I);
80
81 // If we didn't find a main function, use the external call graph node
82 if (Root == 0)
83 Root = ExternalCallingNode;
84
85 return false;
86}
87
88INITIALIZE_PASS(CallGraph, "basiccg", "CallGraph Construction", false, true)
89
90char CallGraph::ID = 0;
91
92void CallGraph::releaseMemory() {
93 /// CallsExternalNode is not in the function map, delete it explicitly.
94 if (CallsExternalNode) {
95 CallsExternalNode->allReferencesDropped();
96 delete CallsExternalNode;
97 CallsExternalNode = 0;
98 }
99
100 if (FunctionMap.empty())
101 return;
102
103// Reset all node's use counts to zero before deleting them to prevent an
104// assertion from firing.
Chris Lattneraedb8a32010-04-20 00:47:34 +0000105#ifndef NDEBUG
106 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
107 I != E; ++I)
108 I->second->allReferencesDropped();
109#endif
110
Chris Lattner081375b2009-08-31 03:15:49 +0000111 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
112 I != E; ++I)
113 delete I->second;
114 FunctionMap.clear();
Chris Lattner03946cd2001-11-26 18:51:25 +0000115}
116
Rafael Espindola6554e5a2013-10-31 03:03:55 +0000117void CallGraph::print(raw_ostream &OS, const Module*) const {
118 OS << "CallGraph Root is: ";
119 if (Function *F = Root->getFunction())
120 OS << F->getName() << "\n";
121 else {
122 OS << "<<null function: 0x" << Root << ">>\n";
123 }
124
Chris Lattnerb9d55472002-11-04 00:21:19 +0000125 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
Chris Lattner6b5411022004-08-08 03:27:49 +0000126 I->second->print(OS);
127}
Manman Ren49d684e2012-09-12 05:06:18 +0000128#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Chris Lattner69349a52009-08-30 22:24:32 +0000129void CallGraph::dump() const {
David Greeneba44b3e2009-12-23 20:03:58 +0000130 print(dbgs(), 0);
Chris Lattner69349a52009-08-30 22:24:32 +0000131}
Manman Renc3366cc2012-09-06 19:55:56 +0000132#endif
Chris Lattner6b5411022004-08-08 03:27:49 +0000133
Chris Lattner03946cd2001-11-26 18:51:25 +0000134//===----------------------------------------------------------------------===//
135// Implementations of public modification methods
136//
137
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000138// removeFunctionFromModule - Unlink the function from this module, returning
139// it. Because this removes the function from the module, the call graph node
140// is destroyed. This is only valid if the function does not call any other
141// functions (ie, there are no edges in it's CGN). The easiest way to do this
Chris Lattner03946cd2001-11-26 18:51:25 +0000142// is to dropAllReferences before calling this.
143//
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000144Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
Chris Lattner4275cf22009-08-31 02:24:20 +0000145 assert(CGN->empty() && "Cannot remove function from call "
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000146 "graph if it references other functions!");
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000147 Function *F = CGN->getFunction(); // Get the function for the call graph node
Chris Lattner79b0c7d2002-07-18 04:43:16 +0000148 delete CGN; // Delete the call graph node for this func
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000149 FunctionMap.erase(F); // Remove the call graph node from the map
Chris Lattner03946cd2001-11-26 18:51:25 +0000150
Chris Lattner6f5e18d2003-08-31 20:36:52 +0000151 Mod->getFunctionList().remove(F);
152 return F;
Chris Lattner03946cd2001-11-26 18:51:25 +0000153}
154
Nick Lewycky0f87ca72011-01-03 03:19:35 +0000155/// spliceFunction - Replace the function represented by this node by another.
156/// This does not rescan the body of the function, so it is suitable when
157/// splicing the body of the old function to the new while also updating all
158/// callers from old to new.
159///
160void CallGraph::spliceFunction(const Function *From, const Function *To) {
161 assert(FunctionMap.count(From) && "No CallGraphNode for function!");
162 assert(!FunctionMap.count(To) &&
163 "Pointing CallGraphNode at a function that already exists");
164 FunctionMapTy::iterator I = FunctionMap.find(From);
165 I->second->F = const_cast<Function*>(To);
166 FunctionMap[To] = I->second;
167 FunctionMap.erase(I);
168}
169
Chris Lattner00ca8d22006-01-14 20:03:00 +0000170// getOrInsertFunction - This method is identical to calling operator[], but
171// it will insert a new CallGraphNode for the specified function if one does
172// not already exist.
173CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
174 CallGraphNode *&CGN = FunctionMap[F];
175 if (CGN) return CGN;
176
177 assert((!F || F->getParent() == Mod) && "Function not in current module!");
178 return CGN = new CallGraphNode(const_cast<Function*>(F));
179}
180
Chris Lattner13626022009-08-23 06:03:38 +0000181void CallGraphNode::print(raw_ostream &OS) const {
Chris Lattnerbe1987772005-12-22 06:07:52 +0000182 if (Function *F = getFunction())
Chris Lattner081375b2009-08-31 03:15:49 +0000183 OS << "Call graph node for function: '" << F->getName() << "'";
Chris Lattnerbe1987772005-12-22 06:07:52 +0000184 else
Chris Lattner081375b2009-08-31 03:15:49 +0000185 OS << "Call graph node <<null function>>";
186
Chris Lattner8c562542010-04-23 18:23:40 +0000187 OS << "<<" << this << ">> #uses=" << getNumReferences() << '\n';
Chris Lattnerbe1987772005-12-22 06:07:52 +0000188
Chris Lattner8c562542010-04-23 18:23:40 +0000189 for (const_iterator I = begin(), E = end(); I != E; ++I) {
190 OS << " CS<" << I->first << "> calls ";
Gabor Greif191812f2009-01-14 17:09:04 +0000191 if (Function *FI = I->second->getFunction())
Chris Lattner8c562542010-04-23 18:23:40 +0000192 OS << "function '" << FI->getName() <<"'\n";
193 else
194 OS << "external node\n";
195 }
196 OS << '\n';
Chris Lattnerbe1987772005-12-22 06:07:52 +0000197}
198
Manman Ren49d684e2012-09-12 05:06:18 +0000199#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
David Greeneba44b3e2009-12-23 20:03:58 +0000200void CallGraphNode::dump() const { print(dbgs()); }
Manman Renc3366cc2012-09-06 19:55:56 +0000201#endif
Chris Lattnerbe1987772005-12-22 06:07:52 +0000202
Chris Lattnercc9709c2008-04-13 19:41:25 +0000203/// removeCallEdgeFor - This method removes the edge in the node for the
204/// specified call site. Note that this method takes linear time, so it
205/// should be used sparingly.
206void CallGraphNode::removeCallEdgeFor(CallSite CS) {
Chris Lattnereedcd842009-08-31 07:23:46 +0000207 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
208 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
Chris Lattner063d0652009-09-01 06:31:31 +0000209 if (I->first == CS.getInstruction()) {
Chris Lattnereedcd842009-08-31 07:23:46 +0000210 I->second->DropRef();
211 *I = CalledFunctions.back();
212 CalledFunctions.pop_back();
213 return;
214 }
Chris Lattnercc9709c2008-04-13 19:41:25 +0000215 }
216}
217
Chris Lattner824a2182004-09-18 21:34:34 +0000218// removeAnyCallEdgeTo - This method removes any call edges from this node to
219// the specified callee function. This takes more time to execute than
220// removeCallEdgeTo, so it should not be used unless necessary.
221void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
Chris Lattnerd6d99df2004-09-19 19:01:06 +0000222 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
Chris Lattner5de3b8b2006-07-12 18:29:36 +0000223 if (CalledFunctions[i].second == Callee) {
Chris Lattner081375b2009-08-31 03:15:49 +0000224 Callee->DropRef();
Chris Lattnerd6d99df2004-09-19 19:01:06 +0000225 CalledFunctions[i] = CalledFunctions.back();
226 CalledFunctions.pop_back();
227 --i; --e;
Chris Lattner824a2182004-09-18 21:34:34 +0000228 }
229}
Reid Spencerbe535662006-06-07 22:00:26 +0000230
Duncan Sands3a813a52008-10-03 07:36:09 +0000231/// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite
232/// from this node to the specified callee function.
233void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) {
Gabor Greif20b722f2009-01-17 19:46:01 +0000234 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
235 assert(I != CalledFunctions.end() && "Cannot find callee to remove!");
236 CallRecord &CR = *I;
Chris Lattner063d0652009-09-01 06:31:31 +0000237 if (CR.second == Callee && CR.first == 0) {
Chris Lattner081375b2009-08-31 03:15:49 +0000238 Callee->DropRef();
239 *I = CalledFunctions.back();
240 CalledFunctions.pop_back();
Duncan Sands3a813a52008-10-03 07:36:09 +0000241 return;
242 }
243 }
244}
245
Chris Lattnere0987212009-09-15 05:40:35 +0000246/// replaceCallEdge - This method replaces the edge in the node for the
247/// specified call site with a new one. Note that this method takes linear
248/// time, so it should be used sparingly.
249void CallGraphNode::replaceCallEdge(CallSite CS,
250 CallSite NewCS, CallGraphNode *NewNode){
251 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
252 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
253 if (I->first == CS.getInstruction()) {
254 I->second->DropRef();
255 I->first = NewCS.getInstruction();
256 I->second = NewNode;
257 NewNode->AddRef();
258 return;
259 }
260 }
261}
262
Reid Spencerbe535662006-06-07 22:00:26 +0000263// Enuse that users of CallGraph.h also link with this file
264DEFINING_FILE_FOR(CallGraph)