PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/external/llvm/lib/Analysis/IPA/CallGraph.cpp

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk
C++ | 337 lines | 209 code | 49 blank | 79 comment | 61 complexity | 9bcd58d2a1997b82b6c51c75cd997687 MD5 | raw file
  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/Debug.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. using namespace llvm;
  22. namespace {
  23. //===----------------------------------------------------------------------===//
  24. // BasicCallGraph class definition
  25. //
  26. class BasicCallGraph : public ModulePass, public CallGraph {
  27. // Root is root of the call graph, or the external node if a 'main' function
  28. // couldn't be found.
  29. //
  30. CallGraphNode *Root;
  31. // ExternalCallingNode - This node has edges to all external functions and
  32. // those internal functions that have their address taken.
  33. CallGraphNode *ExternalCallingNode;
  34. // CallsExternalNode - This node has edges to it from all functions making
  35. // indirect calls or calling an external function.
  36. CallGraphNode *CallsExternalNode;
  37. public:
  38. static char ID; // Class identification, replacement for typeinfo
  39. BasicCallGraph() : ModulePass(ID), Root(0),
  40. ExternalCallingNode(0), CallsExternalNode(0) {
  41. initializeBasicCallGraphPass(*PassRegistry::getPassRegistry());
  42. }
  43. // runOnModule - Compute the call graph for the specified module.
  44. virtual bool runOnModule(Module &M) {
  45. CallGraph::initialize(M);
  46. ExternalCallingNode = getOrInsertFunction(0);
  47. CallsExternalNode = new CallGraphNode(0);
  48. Root = 0;
  49. // Add every function to the call graph.
  50. for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
  51. addToCallGraph(I);
  52. // If we didn't find a main function, use the external call graph node
  53. if (Root == 0) Root = ExternalCallingNode;
  54. return false;
  55. }
  56. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  57. AU.setPreservesAll();
  58. }
  59. virtual void print(raw_ostream &OS, const Module *) const {
  60. OS << "CallGraph Root is: ";
  61. if (Function *F = getRoot()->getFunction())
  62. OS << F->getName() << "\n";
  63. else {
  64. OS << "<<null function: 0x" << getRoot() << ">>\n";
  65. }
  66. CallGraph::print(OS, 0);
  67. }
  68. virtual void releaseMemory() {
  69. destroy();
  70. }
  71. /// getAdjustedAnalysisPointer - This method is used when a pass implements
  72. /// an analysis interface through multiple inheritance. If needed, it should
  73. /// override this to adjust the this pointer as needed for the specified pass
  74. /// info.
  75. virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
  76. if (PI == &CallGraph::ID)
  77. return (CallGraph*)this;
  78. return this;
  79. }
  80. CallGraphNode* getExternalCallingNode() const { return ExternalCallingNode; }
  81. CallGraphNode* getCallsExternalNode() const { return CallsExternalNode; }
  82. // getRoot - Return the root of the call graph, which is either main, or if
  83. // main cannot be found, the external node.
  84. //
  85. CallGraphNode *getRoot() { return Root; }
  86. const CallGraphNode *getRoot() const { return Root; }
  87. private:
  88. //===---------------------------------------------------------------------
  89. // Implementation of CallGraph construction
  90. //
  91. // addToCallGraph - Add a function to the call graph, and link the node to all
  92. // of the functions that it calls.
  93. //
  94. void addToCallGraph(Function *F) {
  95. CallGraphNode *Node = getOrInsertFunction(F);
  96. // If this function has external linkage, anything could call it.
  97. if (!F->hasLocalLinkage()) {
  98. ExternalCallingNode->addCalledFunction(CallSite(), Node);
  99. // Found the entry point?
  100. if (F->getName() == "main") {
  101. if (Root) // Found multiple external mains? Don't pick one.
  102. Root = ExternalCallingNode;
  103. else
  104. Root = Node; // Found a main, keep track of it!
  105. }
  106. }
  107. // If this function has its address taken, anything could call it.
  108. if (F->hasAddressTaken())
  109. ExternalCallingNode->addCalledFunction(CallSite(), Node);
  110. // If this function is not defined in this translation unit, it could call
  111. // anything.
  112. if (F->isDeclaration() && !F->isIntrinsic())
  113. Node->addCalledFunction(CallSite(), CallsExternalNode);
  114. // Look for calls by this function.
  115. for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
  116. for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
  117. II != IE; ++II) {
  118. CallSite CS(cast<Value>(II));
  119. if (CS && !isa<IntrinsicInst>(II)) {
  120. const Function *Callee = CS.getCalledFunction();
  121. if (Callee)
  122. Node->addCalledFunction(CS, getOrInsertFunction(Callee));
  123. else
  124. Node->addCalledFunction(CS, CallsExternalNode);
  125. }
  126. }
  127. }
  128. //
  129. // destroy - Release memory for the call graph
  130. virtual void destroy() {
  131. /// CallsExternalNode is not in the function map, delete it explicitly.
  132. if (CallsExternalNode) {
  133. CallsExternalNode->allReferencesDropped();
  134. delete CallsExternalNode;
  135. CallsExternalNode = 0;
  136. }
  137. CallGraph::destroy();
  138. }
  139. };
  140. } //End anonymous namespace
  141. INITIALIZE_ANALYSIS_GROUP(CallGraph, "Call Graph", BasicCallGraph)
  142. INITIALIZE_AG_PASS(BasicCallGraph, CallGraph, "basiccg",
  143. "Basic CallGraph Construction", false, true, true)
  144. char CallGraph::ID = 0;
  145. char BasicCallGraph::ID = 0;
  146. void CallGraph::initialize(Module &M) {
  147. Mod = &M;
  148. }
  149. void CallGraph::destroy() {
  150. if (FunctionMap.empty()) return;
  151. // Reset all node's use counts to zero before deleting them to prevent an
  152. // assertion from firing.
  153. #ifndef NDEBUG
  154. for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
  155. I != E; ++I)
  156. I->second->allReferencesDropped();
  157. #endif
  158. for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
  159. I != E; ++I)
  160. delete I->second;
  161. FunctionMap.clear();
  162. }
  163. void CallGraph::print(raw_ostream &OS, Module*) const {
  164. for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I)
  165. I->second->print(OS);
  166. }
  167. #ifndef NDEBUG
  168. void CallGraph::dump() const {
  169. print(dbgs(), 0);
  170. }
  171. #endif
  172. //===----------------------------------------------------------------------===//
  173. // Implementations of public modification methods
  174. //
  175. // removeFunctionFromModule - Unlink the function from this module, returning
  176. // it. Because this removes the function from the module, the call graph node
  177. // is destroyed. This is only valid if the function does not call any other
  178. // functions (ie, there are no edges in it's CGN). The easiest way to do this
  179. // is to dropAllReferences before calling this.
  180. //
  181. Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
  182. assert(CGN->empty() && "Cannot remove function from call "
  183. "graph if it references other functions!");
  184. Function *F = CGN->getFunction(); // Get the function for the call graph node
  185. delete CGN; // Delete the call graph node for this func
  186. FunctionMap.erase(F); // Remove the call graph node from the map
  187. Mod->getFunctionList().remove(F);
  188. return F;
  189. }
  190. /// spliceFunction - Replace the function represented by this node by another.
  191. /// This does not rescan the body of the function, so it is suitable when
  192. /// splicing the body of the old function to the new while also updating all
  193. /// callers from old to new.
  194. ///
  195. void CallGraph::spliceFunction(const Function *From, const Function *To) {
  196. assert(FunctionMap.count(From) && "No CallGraphNode for function!");
  197. assert(!FunctionMap.count(To) &&
  198. "Pointing CallGraphNode at a function that already exists");
  199. FunctionMapTy::iterator I = FunctionMap.find(From);
  200. I->second->F = const_cast<Function*>(To);
  201. FunctionMap[To] = I->second;
  202. FunctionMap.erase(I);
  203. }
  204. // getOrInsertFunction - This method is identical to calling operator[], but
  205. // it will insert a new CallGraphNode for the specified function if one does
  206. // not already exist.
  207. CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
  208. CallGraphNode *&CGN = FunctionMap[F];
  209. if (CGN) return CGN;
  210. assert((!F || F->getParent() == Mod) && "Function not in current module!");
  211. return CGN = new CallGraphNode(const_cast<Function*>(F));
  212. }
  213. void CallGraphNode::print(raw_ostream &OS) const {
  214. if (Function *F = getFunction())
  215. OS << "Call graph node for function: '" << F->getName() << "'";
  216. else
  217. OS << "Call graph node <<null function>>";
  218. OS << "<<" << this << ">> #uses=" << getNumReferences() << '\n';
  219. for (const_iterator I = begin(), E = end(); I != E; ++I) {
  220. OS << " CS<" << I->first << "> calls ";
  221. if (Function *FI = I->second->getFunction())
  222. OS << "function '" << FI->getName() <<"'\n";
  223. else
  224. OS << "external node\n";
  225. }
  226. OS << '\n';
  227. }
  228. #ifndef NDEBUG
  229. void CallGraphNode::dump() const { print(dbgs()); }
  230. #endif
  231. /// removeCallEdgeFor - This method removes the edge in the node for the
  232. /// specified call site. Note that this method takes linear time, so it
  233. /// should be used sparingly.
  234. void CallGraphNode::removeCallEdgeFor(CallSite CS) {
  235. for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
  236. assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
  237. if (I->first == CS.getInstruction()) {
  238. I->second->DropRef();
  239. *I = CalledFunctions.back();
  240. CalledFunctions.pop_back();
  241. return;
  242. }
  243. }
  244. }
  245. // removeAnyCallEdgeTo - This method removes any call edges from this node to
  246. // the specified callee function. This takes more time to execute than
  247. // removeCallEdgeTo, so it should not be used unless necessary.
  248. void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
  249. for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
  250. if (CalledFunctions[i].second == Callee) {
  251. Callee->DropRef();
  252. CalledFunctions[i] = CalledFunctions.back();
  253. CalledFunctions.pop_back();
  254. --i; --e;
  255. }
  256. }
  257. /// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite
  258. /// from this node to the specified callee function.
  259. void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) {
  260. for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
  261. assert(I != CalledFunctions.end() && "Cannot find callee to remove!");
  262. CallRecord &CR = *I;
  263. if (CR.second == Callee && CR.first == 0) {
  264. Callee->DropRef();
  265. *I = CalledFunctions.back();
  266. CalledFunctions.pop_back();
  267. return;
  268. }
  269. }
  270. }
  271. /// replaceCallEdge - This method replaces the edge in the node for the
  272. /// specified call site with a new one. Note that this method takes linear
  273. /// time, so it should be used sparingly.
  274. void CallGraphNode::replaceCallEdge(CallSite CS,
  275. CallSite NewCS, CallGraphNode *NewNode){
  276. for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
  277. assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
  278. if (I->first == CS.getInstruction()) {
  279. I->second->DropRef();
  280. I->first = NewCS.getInstruction();
  281. I->second = NewNode;
  282. NewNode->AddRef();
  283. return;
  284. }
  285. }
  286. }
  287. // Enuse that users of CallGraph.h also link with this file
  288. DEFINING_FILE_FOR(CallGraph)