1 //===- CallGraph.cpp - Build a Module's call graph ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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/Streams.h"
23 /// isOnlyADirectCall - Return true if this callsite is *just* a direct call to
24 /// the specified function. Specifically return false if the callsite also
25 /// takes the address of the function.
26 static bool isOnlyADirectCall(Function *F, CallSite CS) {
27 if (!CS.getInstruction()) return false;
28 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; ++I)
29 if (*I == F) return false;
35 //===----------------------------------------------------------------------===//
36 // BasicCallGraph class definition
38 class BasicCallGraph : public CallGraph, public ModulePass {
39 // Root is root of the call graph, or the external node if a 'main' function
44 // ExternalCallingNode - This node has edges to all external functions and
45 // those internal functions that have their address taken.
46 CallGraphNode *ExternalCallingNode;
48 // CallsExternalNode - This node has edges to it from all functions making
49 // indirect calls or calling an external function.
50 CallGraphNode *CallsExternalNode;
53 BasicCallGraph() : Root(0), ExternalCallingNode(0), CallsExternalNode(0) {}
55 // runOnModule - Compute the call graph for the specified module.
56 virtual bool runOnModule(Module &M) {
57 CallGraph::initialize(M);
59 ExternalCallingNode = getOrInsertFunction(0);
60 CallsExternalNode = new CallGraphNode(0);
63 // Add every function to the call graph...
64 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
67 // If we didn't find a main function, use the external call graph node
68 if (Root == 0) Root = ExternalCallingNode;
73 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
77 void print(OStream &o, const Module *M) const {
78 if (o.stream()) print(*o.stream(), M);
81 virtual void print(std::ostream &o, const Module *M) const {
82 o << "CallGraph Root is: ";
83 if (Function *F = getRoot()->getFunction())
84 o << F->getName() << "\n";
86 o << "<<null function: 0x" << getRoot() << ">>\n";
88 CallGraph::print(o, M);
91 virtual void releaseMemory() {
95 /// dump - Print out this call graph.
97 inline void dump() const {
101 CallGraphNode* getExternalCallingNode() const { return ExternalCallingNode; }
102 CallGraphNode* getCallsExternalNode() const { return CallsExternalNode; }
104 // getRoot - Return the root of the call graph, which is either main, or if
105 // main cannot be found, the external node.
107 CallGraphNode *getRoot() { return Root; }
108 const CallGraphNode *getRoot() const { return Root; }
111 //===---------------------------------------------------------------------
112 // Implementation of CallGraph construction
115 // addToCallGraph - Add a function to the call graph, and link the node to all
116 // of the functions that it calls.
118 void addToCallGraph(Function *F) {
119 CallGraphNode *Node = getOrInsertFunction(F);
121 // If this function has external linkage, anything could call it.
122 if (!F->hasInternalLinkage()) {
123 ExternalCallingNode->addCalledFunction(CallSite(), Node);
125 // Found the entry point?
126 if (F->getName() == "main") {
127 if (Root) // Found multiple external mains? Don't pick one.
128 Root = ExternalCallingNode;
130 Root = Node; // Found a main, keep track of it!
134 // If this function is not defined in this translation unit, it could call
136 if (F->isExternal() && !F->getIntrinsicID())
137 Node->addCalledFunction(CallSite(), CallsExternalNode);
139 // Loop over all of the users of the function... looking for callers...
141 bool isUsedExternally = false;
142 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I){
143 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
144 CallSite CS = CallSite::get(Inst);
145 if (isOnlyADirectCall(F, CS))
146 getOrInsertFunction(Inst->getParent()->getParent())
147 ->addCalledFunction(CS, Node);
149 isUsedExternally = true;
150 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(*I)) {
151 for (Value::use_iterator I = GV->use_begin(), E = GV->use_end();
153 if (Instruction *Inst = dyn_cast<Instruction>(*I)) {
154 CallSite CS = CallSite::get(Inst);
155 if (isOnlyADirectCall(F, CS))
156 getOrInsertFunction(Inst->getParent()->getParent())
157 ->addCalledFunction(CS, Node);
159 isUsedExternally = true;
161 isUsedExternally = true;
163 } else { // Can't classify the user!
164 isUsedExternally = true;
167 if (isUsedExternally)
168 ExternalCallingNode->addCalledFunction(CallSite(), Node);
170 // Look for an indirect function call.
171 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
172 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
174 CallSite CS = CallSite::get(II);
175 if (CS.getInstruction() && !CS.getCalledFunction())
176 Node->addCalledFunction(CS, CallsExternalNode);
181 // destroy - Release memory for the call graph
182 virtual void destroy() {
183 /// CallsExternalNode is not in the function map, delete it explicitly.
184 delete CallsExternalNode;
185 CallsExternalNode = 0;
186 CallGraph::destroy();
190 RegisterAnalysisGroup<CallGraph> X("Call Graph");
191 RegisterPass<BasicCallGraph> Y("basiccg", "Basic CallGraph Construction");
192 RegisterAnalysisGroup<CallGraph, true> Z(Y);
194 } //End anonymous namespace
196 void CallGraph::initialize(Module &M) {
200 void CallGraph::destroy() {
201 if (!FunctionMap.empty()) {
202 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
209 void CallGraph::print(std::ostream &OS, const Module *M) const {
210 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
211 I->second->print(OS);
214 void CallGraph::dump() const {
218 //===----------------------------------------------------------------------===//
219 // Implementations of public modification methods
222 // removeFunctionFromModule - Unlink the function from this module, returning
223 // it. Because this removes the function from the module, the call graph node
224 // is destroyed. This is only valid if the function does not call any other
225 // functions (ie, there are no edges in it's CGN). The easiest way to do this
226 // is to dropAllReferences before calling this.
228 Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
229 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call "
230 "graph if it references other functions!");
231 Function *F = CGN->getFunction(); // Get the function for the call graph node
232 delete CGN; // Delete the call graph node for this func
233 FunctionMap.erase(F); // Remove the call graph node from the map
235 Mod->getFunctionList().remove(F);
239 // changeFunction - This method changes the function associated with this
240 // CallGraphNode, for use by transformations that need to change the prototype
241 // of a Function (thus they must create a new Function and move the old code
243 void CallGraph::changeFunction(Function *OldF, Function *NewF) {
244 iterator I = FunctionMap.find(OldF);
245 CallGraphNode *&New = FunctionMap[NewF];
246 assert(I != FunctionMap.end() && I->second && !New &&
247 "OldF didn't exist in CG or NewF already does!");
250 FunctionMap.erase(I);
253 // getOrInsertFunction - This method is identical to calling operator[], but
254 // it will insert a new CallGraphNode for the specified function if one does
255 // not already exist.
256 CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
257 CallGraphNode *&CGN = FunctionMap[F];
260 assert((!F || F->getParent() == Mod) && "Function not in current module!");
261 return CGN = new CallGraphNode(const_cast<Function*>(F));
264 void CallGraphNode::print(std::ostream &OS) const {
265 if (Function *F = getFunction())
266 OS << "Call graph node for function: '" << F->getName() <<"'\n";
268 OS << "Call graph node <<null function: 0x" << this << ">>:\n";
270 for (const_iterator I = begin(), E = end(); I != E; ++I)
271 if (I->second->getFunction())
272 OS << " Calls function '" << I->second->getFunction()->getName() <<"'\n";
274 OS << " Calls external node\n";
278 void CallGraphNode::dump() const { print(cerr); }
280 void CallGraphNode::removeCallEdgeTo(CallGraphNode *Callee) {
281 for (unsigned i = CalledFunctions.size(); ; --i) {
282 assert(i && "Cannot find callee to remove!");
283 if (CalledFunctions[i-1].second == Callee) {
284 CalledFunctions.erase(CalledFunctions.begin()+i-1);
290 // removeAnyCallEdgeTo - This method removes any call edges from this node to
291 // the specified callee function. This takes more time to execute than
292 // removeCallEdgeTo, so it should not be used unless necessary.
293 void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
294 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
295 if (CalledFunctions[i].second == Callee) {
296 CalledFunctions[i] = CalledFunctions.back();
297 CalledFunctions.pop_back();
302 // Enuse that users of CallGraph.h also link with this file
303 DEFINING_FILE_FOR(CallGraph)