CallGraph.cpp 10 KB

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