1 //===- CallGraph.cpp - Build a Module's call graph ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the CallGraph class and provides the BasicCallGraph
11 // default implementation.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/CallGraph.h"
16 #include "llvm/Module.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/Support/CallSite.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/Streams.h"
26 //===----------------------------------------------------------------------===//
27 // BasicCallGraph class definition
29 class VISIBILITY_HIDDEN BasicCallGraph : public CallGraph, public ModulePass {
30 // Root is root of the call graph, or the external node if a 'main' function
35 // ExternalCallingNode - This node has edges to all external functions and
36 // those internal functions that have their address taken.
37 CallGraphNode *ExternalCallingNode;
39 // CallsExternalNode - This node has edges to it from all functions making
40 // indirect calls or calling an external function.
41 CallGraphNode *CallsExternalNode;
44 static char ID; // Class identification, replacement for typeinfo
45 BasicCallGraph() : ModulePass(&ID), Root(0),
46 ExternalCallingNode(0), CallsExternalNode(0) {}
48 // runOnModule - Compute the call graph for the specified module.
49 virtual bool runOnModule(Module &M) {
50 CallGraph::initialize(M);
52 ExternalCallingNode = getOrInsertFunction(0);
53 CallsExternalNode = new CallGraphNode(0);
56 // Add every function to the call graph...
57 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
60 // If we didn't find a main function, use the external call graph node
61 if (Root == 0) Root = ExternalCallingNode;
66 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
70 void print(std::ostream *o, const Module *M) const {
74 virtual void print(std::ostream &o, const Module *M) const {
75 o << "CallGraph Root is: ";
76 if (Function *F = getRoot()->getFunction())
77 o << F->getName() << "\n";
79 o << "<<null function: 0x" << getRoot() << ">>\n";
81 CallGraph::print(o, M);
84 virtual void releaseMemory() {
88 /// dump - Print out this call graph.
90 inline void dump() const {
94 CallGraphNode* getExternalCallingNode() const { return ExternalCallingNode; }
95 CallGraphNode* getCallsExternalNode() const { return CallsExternalNode; }
97 // getRoot - Return the root of the call graph, which is either main, or if
98 // main cannot be found, the external node.
100 CallGraphNode *getRoot() { return Root; }
101 const CallGraphNode *getRoot() const { return Root; }
104 //===---------------------------------------------------------------------
105 // Implementation of CallGraph construction
108 // addToCallGraph - Add a function to the call graph, and link the node to all
109 // of the functions that it calls.
111 void addToCallGraph(Function *F) {
112 CallGraphNode *Node = getOrInsertFunction(F);
114 // If this function has external linkage, anything could call it.
115 if (!F->hasInternalLinkage()) {
116 ExternalCallingNode->addCalledFunction(CallSite(), Node);
118 // Found the entry point?
119 if (F->getName() == "main") {
120 if (Root) // Found multiple external mains? Don't pick one.
121 Root = ExternalCallingNode;
123 Root = Node; // Found a main, keep track of it!
127 // If this function is not defined in this translation unit, it could call
129 if (F->isDeclaration() && !F->isIntrinsic())
130 Node->addCalledFunction(CallSite(), CallsExternalNode);
132 // Loop over all of the users of the function, looking for non-call uses.
133 bool isUsedExternally = false;
134 for (Value::use_iterator I = F->use_begin(), E = F->use_end();
135 I != E && !isUsedExternally; ++I) {
136 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
137 CallSite CS = CallSite::get(Inst);
138 // Not a call? Or F being passed as a parameter not as the callee?
139 isUsedExternally = !CS.getInstruction() || I.getOperandNo();
140 } else { // User is not a direct call!
141 isUsedExternally = true;
144 if (isUsedExternally)
145 ExternalCallingNode->addCalledFunction(CallSite(), Node);
147 // Look for calls by this function.
148 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
149 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
151 CallSite CS = CallSite::get(II);
152 if (CS.getInstruction()) {
153 const Function *Callee = CS.getCalledFunction();
155 Node->addCalledFunction(CS, getOrInsertFunction(Callee));
157 Node->addCalledFunction(CS, CallsExternalNode);
163 // destroy - Release memory for the call graph
164 virtual void destroy() {
165 /// CallsExternalNode is not in the function map, delete it explicitly.
166 delete CallsExternalNode;
167 CallsExternalNode = 0;
168 CallGraph::destroy();
172 } //End anonymous namespace
174 static RegisterAnalysisGroup<CallGraph> X("Call Graph");
175 static RegisterPass<BasicCallGraph>
176 Y("basiccg", "Basic CallGraph Construction", false, true);
177 static RegisterAnalysisGroup<CallGraph, true> Z(Y);
179 char CallGraph::ID = 0;
180 char BasicCallGraph::ID = 0;
182 void CallGraph::initialize(Module &M) {
186 void CallGraph::destroy() {
187 if (!FunctionMap.empty()) {
188 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
195 void CallGraph::print(std::ostream &OS, const Module *M) const {
196 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
197 I->second->print(OS);
200 void CallGraph::dump() const {
204 //===----------------------------------------------------------------------===//
205 // Implementations of public modification methods
208 // removeFunctionFromModule - Unlink the function from this module, returning
209 // it. Because this removes the function from the module, the call graph node
210 // is destroyed. This is only valid if the function does not call any other
211 // functions (ie, there are no edges in it's CGN). The easiest way to do this
212 // is to dropAllReferences before calling this.
214 Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
215 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
216 "graph if it references other functions!");
217 Function *F = CGN->getFunction(); // Get the function for the call graph node
218 delete CGN; // Delete the call graph node for this func
219 FunctionMap.erase(F); // Remove the call graph node from the map
221 Mod->getFunctionList().remove(F);
225 // changeFunction - This method changes the function associated with this
226 // CallGraphNode, for use by transformations that need to change the prototype
227 // of a Function (thus they must create a new Function and move the old code
229 void CallGraph::changeFunction(Function *OldF, Function *NewF) {
230 iterator I = FunctionMap.find(OldF);
231 CallGraphNode *&New = FunctionMap[NewF];
232 assert(I != FunctionMap.end() && I->second && !New &&
233 "OldF didn't exist in CG or NewF already does!");
236 FunctionMap.erase(I);
239 // getOrInsertFunction - This method is identical to calling operator[], but
240 // it will insert a new CallGraphNode for the specified function if one does
241 // not already exist.
242 CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
243 CallGraphNode *&CGN = FunctionMap[F];
246 assert((!F || F->getParent() == Mod) && "Function not in current module!");
247 return CGN = new CallGraphNode(const_cast<Function*>(F));
250 void CallGraphNode::print(std::ostream &OS) const {
251 if (Function *F = getFunction())
252 OS << "Call graph node for function: '" << F->getName() <<"'\n";
254 OS << "Call graph node <<null function: 0x" << this << ">>:\n";
256 for (const_iterator I = begin(), E = end(); I != E; ++I)
257 if (I->second->getFunction())
258 OS << " Calls function '" << I->second->getFunction()->getName() <<"'\n";
260 OS << " Calls external node\n";
264 void CallGraphNode::dump() const { print(cerr); }
266 /// removeCallEdgeFor - This method removes the edge in the node for the
267 /// specified call site. Note that this method takes linear time, so it
268 /// should be used sparingly.
269 void CallGraphNode::removeCallEdgeFor(CallSite CS) {
270 for (unsigned i = CalledFunctions.size(); ; --i) {
271 assert(i && "Cannot find callsite to remove!");
272 if (CalledFunctions[i-1].first == CS) {
273 CalledFunctions.erase(CalledFunctions.begin()+i-1);
280 // removeAnyCallEdgeTo - This method removes any call edges from this node to
281 // the specified callee function. This takes more time to execute than
282 // removeCallEdgeTo, so it should not be used unless necessary.
283 void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
284 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
285 if (CalledFunctions[i].second == Callee) {
286 CalledFunctions[i] = CalledFunctions.back();
287 CalledFunctions.pop_back();
292 /// replaceCallSite - Make the edge in the node for Old CallSite be for
293 /// New CallSite instead. Note that this method takes linear time, so it
294 /// should be used sparingly.
295 void CallGraphNode::replaceCallSite(CallSite Old, CallSite New) {
296 for (unsigned i = CalledFunctions.size(); ; --i) {
297 assert(i && "Cannot find callsite to replace!");
298 if (CalledFunctions[i-1].first == Old) {
299 CalledFunctions[i-1].first = New;
305 // Enuse that users of CallGraph.h also link with this file
306 DEFINING_FILE_FOR(CallGraph)