CallGraph.cpp revision a541b0fde2ab6b8b037edf113d42da41a2c5aae9
1//===- CallGraph.cpp - Build a Module's call graph ------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the CallGraph class and provides the BasicCallGraph 11// default implementation. 12// 13//===----------------------------------------------------------------------===// 14 15#include "llvm/Analysis/CallGraph.h" 16#include "llvm/Module.h" 17#include "llvm/Instructions.h" 18#include "llvm/IntrinsicInst.h" 19#include "llvm/Support/CallSite.h" 20#include "llvm/Support/Compiler.h" 21#include "llvm/Support/raw_ostream.h" 22using namespace llvm; 23 24namespace { 25 26//===----------------------------------------------------------------------===// 27// BasicCallGraph class definition 28// 29class VISIBILITY_HIDDEN BasicCallGraph : public CallGraph, public ModulePass { 30 // Root is root of the call graph, or the external node if a 'main' function 31 // couldn't be found. 32 // 33 CallGraphNode *Root; 34 35 // ExternalCallingNode - This node has edges to all external functions and 36 // those internal functions that have their address taken. 37 CallGraphNode *ExternalCallingNode; 38 39 // CallsExternalNode - This node has edges to it from all functions making 40 // indirect calls or calling an external function. 41 CallGraphNode *CallsExternalNode; 42 43public: 44 static char ID; // Class identification, replacement for typeinfo 45 BasicCallGraph() : ModulePass(&ID), Root(0), 46 ExternalCallingNode(0), CallsExternalNode(0) {} 47 48 // runOnModule - Compute the call graph for the specified module. 49 virtual bool runOnModule(Module &M) { 50 CallGraph::initialize(M); 51 52 ExternalCallingNode = getOrInsertFunction(0); 53 CallsExternalNode = new CallGraphNode(0); 54 Root = 0; 55 56 // Add every function to the call graph. 57 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 58 addToCallGraph(I); 59 60 // If we didn't find a main function, use the external call graph node 61 if (Root == 0) Root = ExternalCallingNode; 62 63 return false; 64 } 65 66 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 67 AU.setPreservesAll(); 68 } 69 70 virtual void print(raw_ostream &OS, const Module *) const { 71 OS << "CallGraph Root is: "; 72 if (Function *F = getRoot()->getFunction()) 73 OS << F->getName() << "\n"; 74 else { 75 OS << "<<null function: 0x" << getRoot() << ">>\n"; 76 } 77 78 CallGraph::print(OS, 0); 79 } 80 81 virtual void releaseMemory() { 82 destroy(); 83 } 84 85 CallGraphNode* getExternalCallingNode() const { return ExternalCallingNode; } 86 CallGraphNode* getCallsExternalNode() const { return CallsExternalNode; } 87 88 // getRoot - Return the root of the call graph, which is either main, or if 89 // main cannot be found, the external node. 90 // 91 CallGraphNode *getRoot() { return Root; } 92 const CallGraphNode *getRoot() const { return Root; } 93 94private: 95 //===--------------------------------------------------------------------- 96 // Implementation of CallGraph construction 97 // 98 99 // addToCallGraph - Add a function to the call graph, and link the node to all 100 // of the functions that it calls. 101 // 102 void addToCallGraph(Function *F) { 103 CallGraphNode *Node = getOrInsertFunction(F); 104 105 // If this function has external linkage, anything could call it. 106 if (!F->hasLocalLinkage()) { 107 ExternalCallingNode->addCalledFunction(CallSite(), Node); 108 109 // Found the entry point? 110 if (F->getName() == "main") { 111 if (Root) // Found multiple external mains? Don't pick one. 112 Root = ExternalCallingNode; 113 else 114 Root = Node; // Found a main, keep track of it! 115 } 116 } 117 118 // Loop over all of the users of the function, looking for non-call uses. 119 for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I) 120 if ((!isa<CallInst>(I) && !isa<InvokeInst>(I)) 121 || !CallSite(cast<Instruction>(I)).isCallee(I)) { 122 // Not a call, or being used as a parameter rather than as the callee. 123 ExternalCallingNode->addCalledFunction(CallSite(), Node); 124 break; 125 } 126 127 // If this function is not defined in this translation unit, it could call 128 // anything. 129 if (F->isDeclaration() && !F->isIntrinsic()) 130 Node->addCalledFunction(CallSite(), CallsExternalNode); 131 132 // Look for calls by this function. 133 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB) 134 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); 135 II != IE; ++II) { 136 CallSite CS = CallSite::get(II); 137 if (CS.getInstruction() && !isa<DbgInfoIntrinsic>(II)) { 138 const Function *Callee = CS.getCalledFunction(); 139 if (Callee) 140 Node->addCalledFunction(CS, getOrInsertFunction(Callee)); 141 else 142 Node->addCalledFunction(CS, CallsExternalNode); 143 } 144 } 145 } 146 147 // 148 // destroy - Release memory for the call graph 149 virtual void destroy() { 150 /// CallsExternalNode is not in the function map, delete it explicitly. 151 delete CallsExternalNode; 152 CallsExternalNode = 0; 153 CallGraph::destroy(); 154 } 155}; 156 157} //End anonymous namespace 158 159static RegisterAnalysisGroup<CallGraph> X("Call Graph"); 160static RegisterPass<BasicCallGraph> 161Y("basiccg", "Basic CallGraph Construction", false, true); 162static RegisterAnalysisGroup<CallGraph, true> Z(Y); 163 164char CallGraph::ID = 0; 165char BasicCallGraph::ID = 0; 166 167void CallGraph::initialize(Module &M) { 168 Mod = &M; 169} 170 171void CallGraph::destroy() { 172 if (FunctionMap.empty()) return; 173 174 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end(); 175 I != E; ++I) 176 delete I->second; 177 FunctionMap.clear(); 178} 179 180void CallGraph::print(raw_ostream &OS, Module*) const { 181 for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I) 182 I->second->print(OS); 183} 184void CallGraph::dump() const { 185 print(errs(), 0); 186} 187 188//===----------------------------------------------------------------------===// 189// Implementations of public modification methods 190// 191 192// removeFunctionFromModule - Unlink the function from this module, returning 193// it. Because this removes the function from the module, the call graph node 194// is destroyed. This is only valid if the function does not call any other 195// functions (ie, there are no edges in it's CGN). The easiest way to do this 196// is to dropAllReferences before calling this. 197// 198Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) { 199 assert(CGN->empty() && "Cannot remove function from call " 200 "graph if it references other functions!"); 201 Function *F = CGN->getFunction(); // Get the function for the call graph node 202 delete CGN; // Delete the call graph node for this func 203 FunctionMap.erase(F); // Remove the call graph node from the map 204 205 Mod->getFunctionList().remove(F); 206 return F; 207} 208 209// getOrInsertFunction - This method is identical to calling operator[], but 210// it will insert a new CallGraphNode for the specified function if one does 211// not already exist. 212CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) { 213 CallGraphNode *&CGN = FunctionMap[F]; 214 if (CGN) return CGN; 215 216 assert((!F || F->getParent() == Mod) && "Function not in current module!"); 217 return CGN = new CallGraphNode(const_cast<Function*>(F)); 218} 219 220void CallGraphNode::print(raw_ostream &OS) const { 221 if (Function *F = getFunction()) 222 OS << "Call graph node for function: '" << F->getName() << "'"; 223 else 224 OS << "Call graph node <<null function>>"; 225 226 OS << "<<0x" << this << ">> #uses=" << getNumReferences() << '\n'; 227 228 for (const_iterator I = begin(), E = end(); I != E; ++I) 229 if (Function *FI = I->second->getFunction()) 230 OS << " Calls function '" << FI->getName() <<"'\n"; 231 else 232 OS << " Calls external node\n"; 233 OS << "\n"; 234} 235 236void CallGraphNode::dump() const { print(errs()); } 237 238/// removeCallEdgeFor - This method removes the edge in the node for the 239/// specified call site. Note that this method takes linear time, so it 240/// should be used sparingly. 241void CallGraphNode::removeCallEdgeFor(CallSite CS) { 242 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { 243 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!"); 244 if (I->first == CS.getInstruction()) { 245 I->second->DropRef(); 246 *I = CalledFunctions.back(); 247 CalledFunctions.pop_back(); 248 return; 249 } 250 } 251} 252 253 254// removeAnyCallEdgeTo - This method removes any call edges from this node to 255// the specified callee function. This takes more time to execute than 256// removeCallEdgeTo, so it should not be used unless necessary. 257void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) { 258 for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i) 259 if (CalledFunctions[i].second == Callee) { 260 Callee->DropRef(); 261 CalledFunctions[i] = CalledFunctions.back(); 262 CalledFunctions.pop_back(); 263 --i; --e; 264 } 265} 266 267/// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite 268/// from this node to the specified callee function. 269void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) { 270 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { 271 assert(I != CalledFunctions.end() && "Cannot find callee to remove!"); 272 CallRecord &CR = *I; 273 if (CR.second == Callee && CR.first == 0) { 274 Callee->DropRef(); 275 *I = CalledFunctions.back(); 276 CalledFunctions.pop_back(); 277 return; 278 } 279 } 280} 281 282/// replaceCallSite - Make the edge in the node for Old CallSite be for 283/// New CallSite instead. Note that this method takes linear time, so it 284/// should be used sparingly. 285void CallGraphNode::replaceCallSite(CallSite Old, CallSite New, 286 CallGraphNode *NewCallee) { 287 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { 288 assert(I != CalledFunctions.end() && "Cannot find callsite to replace!"); 289 if (I->first != Old.getInstruction()) continue; 290 291 I->first = New.getInstruction(); 292 293 // If the callee is changing, not just the callsite, then update it as 294 // well. 295 if (NewCallee) { 296 I->second->DropRef(); 297 I->second = NewCallee; 298 I->second->AddRef(); 299 } 300 return; 301 } 302} 303 304// Enuse that users of CallGraph.h also link with this file 305DEFINING_FILE_FOR(CallGraph) 306