LLVM API Documentation
00001 //===- CallGraph.cpp - Build a Module's call graph ------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the CallGraph class and provides the BasicCallGraph 00011 // default implementation. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/Analysis/CallGraph.h" 00016 #include "llvm/Module.h" 00017 #include "llvm/Instructions.h" 00018 #include "llvm/IntrinsicInst.h" 00019 #include "llvm/Support/CallSite.h" 00020 #include "llvm/Support/Compiler.h" 00021 #include "llvm/Support/Streams.h" 00022 #include <ostream> 00023 using namespace llvm; 00024 00025 namespace { 00026 00027 //===----------------------------------------------------------------------===// 00028 // BasicCallGraph class definition 00029 // 00030 class VISIBILITY_HIDDEN BasicCallGraph : public CallGraph, public ModulePass { 00031 // Root is root of the call graph, or the external node if a 'main' function 00032 // couldn't be found. 00033 // 00034 CallGraphNode *Root; 00035 00036 // ExternalCallingNode - This node has edges to all external functions and 00037 // those internal functions that have their address taken. 00038 CallGraphNode *ExternalCallingNode; 00039 00040 // CallsExternalNode - This node has edges to it from all functions making 00041 // indirect calls or calling an external function. 00042 CallGraphNode *CallsExternalNode; 00043 00044 public: 00045 static char ID; // Class identification, replacement for typeinfo 00046 BasicCallGraph() : ModulePass(&ID), Root(0), 00047 ExternalCallingNode(0), CallsExternalNode(0) {} 00048 00049 // runOnModule - Compute the call graph for the specified module. 00050 virtual bool runOnModule(Module &M) { 00051 CallGraph::initialize(M); 00052 00053 ExternalCallingNode = getOrInsertFunction(0); 00054 CallsExternalNode = new CallGraphNode(0); 00055 Root = 0; 00056 00057 // Add every function to the call graph... 00058 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 00059 addToCallGraph(I); 00060 00061 // If we didn't find a main function, use the external call graph node 00062 if (Root == 0) Root = ExternalCallingNode; 00063 00064 return false; 00065 } 00066 00067 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00068 AU.setPreservesAll(); 00069 } 00070 00071 void print(std::ostream *o, const Module *M) const { 00072 if (o) print(*o, M); 00073 } 00074 00075 virtual void print(std::ostream &o, const Module *M) const { 00076 o << "CallGraph Root is: "; 00077 if (Function *F = getRoot()->getFunction()) 00078 o << F->getNameStr() << "\n"; 00079 else 00080 o << "<<null function: 0x" << getRoot() << ">>\n"; 00081 00082 CallGraph::print(o, M); 00083 } 00084 00085 virtual void releaseMemory() { 00086 destroy(); 00087 } 00088 00089 /// dump - Print out this call graph. 00090 /// 00091 inline void dump() const { 00092 print(cerr, Mod); 00093 } 00094 00095 CallGraphNode* getExternalCallingNode() const { return ExternalCallingNode; } 00096 CallGraphNode* getCallsExternalNode() const { return CallsExternalNode; } 00097 00098 // getRoot - Return the root of the call graph, which is either main, or if 00099 // main cannot be found, the external node. 00100 // 00101 CallGraphNode *getRoot() { return Root; } 00102 const CallGraphNode *getRoot() const { return Root; } 00103 00104 private: 00105 //===--------------------------------------------------------------------- 00106 // Implementation of CallGraph construction 00107 // 00108 00109 // addToCallGraph - Add a function to the call graph, and link the node to all 00110 // of the functions that it calls. 00111 // 00112 void addToCallGraph(Function *F) { 00113 CallGraphNode *Node = getOrInsertFunction(F); 00114 00115 // If this function has external linkage, anything could call it. 00116 if (!F->hasLocalLinkage()) { 00117 ExternalCallingNode->addCalledFunction(CallSite(), Node); 00118 00119 // Found the entry point? 00120 if (F->getName() == "main") { 00121 if (Root) // Found multiple external mains? Don't pick one. 00122 Root = ExternalCallingNode; 00123 else 00124 Root = Node; // Found a main, keep track of it! 00125 } 00126 } 00127 00128 // Loop over all of the users of the function, looking for non-call uses. 00129 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I) 00130 if ((!isa<CallInst>(I) && !isa<InvokeInst>(I)) 00131 || !CallSite(cast<Instruction>(I)).isCallee(I)) { 00132 // Not a call, or being used as a parameter rather than as the callee. 00133 ExternalCallingNode->addCalledFunction(CallSite(), Node); 00134 break; 00135 } 00136 00137 // If this function is not defined in this translation unit, it could call 00138 // anything. 00139 if (F->isDeclaration() && !F->isIntrinsic()) 00140 Node->addCalledFunction(CallSite(), CallsExternalNode); 00141 00142 // Look for calls by this function. 00143 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB) 00144 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); 00145 II != IE; ++II) { 00146 CallSite CS = CallSite::get(II); 00147 if (CS.getInstruction() && !isa<DbgInfoIntrinsic>(II)) { 00148 const Function *Callee = CS.getCalledFunction(); 00149 if (Callee) 00150 Node->addCalledFunction(CS, getOrInsertFunction(Callee)); 00151 else 00152 Node->addCalledFunction(CS, CallsExternalNode); 00153 } 00154 } 00155 } 00156 00157 // 00158 // destroy - Release memory for the call graph 00159 virtual void destroy() { 00160 /// CallsExternalNode is not in the function map, delete it explicitly. 00161 delete CallsExternalNode; 00162 CallsExternalNode = 0; 00163 CallGraph::destroy(); 00164 } 00165 }; 00166 00167 } //End anonymous namespace 00168 00169 static RegisterAnalysisGroup<CallGraph> X("Call Graph"); 00170 static RegisterPass<BasicCallGraph> 00171 Y("basiccg", "Basic CallGraph Construction", false, true); 00172 static RegisterAnalysisGroup<CallGraph, true> Z(Y); 00173 00174 char CallGraph::ID = 0; 00175 char BasicCallGraph::ID = 0; 00176 00177 void CallGraph::initialize(Module &M) { 00178 Mod = &M; 00179 } 00180 00181 void CallGraph::destroy() { 00182 if (!FunctionMap.empty()) { 00183 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end(); 00184 I != E; ++I) 00185 delete I->second; 00186 FunctionMap.clear(); 00187 } 00188 } 00189 00190 void CallGraph::print(std::ostream &OS, const Module *M) const { 00191 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I) 00192 I->second->print(OS); 00193 } 00194 00195 void CallGraph::dump() const { 00196 print(cerr, 0); 00197 } 00198 00199 //===----------------------------------------------------------------------===// 00200 // Implementations of public modification methods 00201 // 00202 00203 // removeFunctionFromModule - Unlink the function from this module, returning 00204 // it. Because this removes the function from the module, the call graph node 00205 // is destroyed. This is only valid if the function does not call any other 00206 // functions (ie, there are no edges in it's CGN). The easiest way to do this 00207 // is to dropAllReferences before calling this. 00208 // 00209 Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) { 00210 assert(CGN->CalledFunctions.empty() && "Cannot remove function from call " 00211 "graph if it references other functions!"); 00212 Function *F = CGN->getFunction(); // Get the function for the call graph node 00213 delete CGN; // Delete the call graph node for this func 00214 FunctionMap.erase(F); // Remove the call graph node from the map 00215 00216 Mod->getFunctionList().remove(F); 00217 return F; 00218 } 00219 00220 // changeFunction - This method changes the function associated with this 00221 // CallGraphNode, for use by transformations that need to change the prototype 00222 // of a Function (thus they must create a new Function and move the old code 00223 // over). 00224 void CallGraph::changeFunction(Function *OldF, Function *NewF) { 00225 iterator I = FunctionMap.find(OldF); 00226 CallGraphNode *&New = FunctionMap[NewF]; 00227 assert(I != FunctionMap.end() && I->second && !New && 00228 "OldF didn't exist in CG or NewF already does!"); 00229 New = I->second; 00230 New->F = NewF; 00231 FunctionMap.erase(I); 00232 } 00233 00234 // getOrInsertFunction - This method is identical to calling operator[], but 00235 // it will insert a new CallGraphNode for the specified function if one does 00236 // not already exist. 00237 CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) { 00238 CallGraphNode *&CGN = FunctionMap[F]; 00239 if (CGN) return CGN; 00240 00241 assert((!F || F->getParent() == Mod) && "Function not in current module!"); 00242 return CGN = new CallGraphNode(const_cast<Function*>(F)); 00243 } 00244 00245 void CallGraphNode::print(std::ostream &OS) const { 00246 if (Function *F = getFunction()) 00247 OS << "Call graph node for function: '" << F->getNameStr() <<"'\n"; 00248 else 00249 OS << "Call graph node <<null function: 0x" << this << ">>:\n"; 00250 00251 for (const_iterator I = begin(), E = end(); I != E; ++I) 00252 if (Function *FI = I->second->getFunction()) 00253 OS << " Calls function '" << FI->getNameStr() <<"'\n"; 00254 else 00255 OS << " Calls external node\n"; 00256 OS << "\n"; 00257 } 00258 00259 void CallGraphNode::dump() const { print(cerr); } 00260 00261 /// removeCallEdgeFor - This method removes the edge in the node for the 00262 /// specified call site. Note that this method takes linear time, so it 00263 /// should be used sparingly. 00264 void CallGraphNode::removeCallEdgeFor(CallSite CS) { 00265 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { 00266 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!"); 00267 if (I->first == CS) { 00268 CalledFunctions.erase(I); 00269 return; 00270 } 00271 } 00272 } 00273 00274 00275 // removeAnyCallEdgeTo - This method removes any call edges from this node to 00276 // the specified callee function. This takes more time to execute than 00277 // removeCallEdgeTo, so it should not be used unless necessary. 00278 void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) { 00279 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i) 00280 if (CalledFunctions[i].second == Callee) { 00281 CalledFunctions[i] = CalledFunctions.back(); 00282 CalledFunctions.pop_back(); 00283 --i; --e; 00284 } 00285 } 00286 00287 /// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite 00288 /// from this node to the specified callee function. 00289 void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) { 00290 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { 00291 assert(I != CalledFunctions.end() && "Cannot find callee to remove!"); 00292 CallRecord &CR = *I; 00293 if (CR.second == Callee && !CR.first.getInstruction()) { 00294 CalledFunctions.erase(I); 00295 return; 00296 } 00297 } 00298 } 00299 00300 /// replaceCallSite - Make the edge in the node for Old CallSite be for 00301 /// New CallSite instead. Note that this method takes linear time, so it 00302 /// should be used sparingly. 00303 void CallGraphNode::replaceCallSite(CallSite Old, CallSite New) { 00304 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { 00305 assert(I != CalledFunctions.end() && "Cannot find callsite to replace!"); 00306 if (I->first == Old) { 00307 I->first = New; 00308 return; 00309 } 00310 } 00311 } 00312 00313 // Enuse that users of CallGraph.h also link with this file 00314 DEFINING_FILE_FOR(CallGraph)