/3rd_party/llvm/include/llvm/Analysis/CallGraph.h

https://code.google.com/p/softart/ · C Header · 379 lines · 173 code · 66 blank · 140 comment · 8 complexity · 3ca59a5474402f055069b72e1a3c2324 MD5 · raw file

  1. //===- CallGraph.h - Build a Module's call graph ----------------*- C++ -*-===//
  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 interface is used to build and manipulate a call graph, which is a very
  11. // useful tool for interprocedural optimization.
  12. //
  13. // Every function in a module is represented as a node in the call graph. The
  14. // callgraph node keeps track of which functions the are called by the function
  15. // corresponding to the node.
  16. //
  17. // A call graph may contain nodes where the function that they correspond to is
  18. // null. These 'external' nodes are used to represent control flow that is not
  19. // represented (or analyzable) in the module. In particular, this analysis
  20. // builds one external node such that:
  21. // 1. All functions in the module without internal linkage will have edges
  22. // from this external node, indicating that they could be called by
  23. // functions outside of the module.
  24. // 2. All functions whose address is used for something more than a direct
  25. // call, for example being stored into a memory location will also have an
  26. // edge from this external node. Since they may be called by an unknown
  27. // caller later, they must be tracked as such.
  28. //
  29. // There is a second external node added for calls that leave this module.
  30. // Functions have a call edge to the external node iff:
  31. // 1. The function is external, reflecting the fact that they could call
  32. // anything without internal linkage or that has its address taken.
  33. // 2. The function contains an indirect function call.
  34. //
  35. // As an extension in the future, there may be multiple nodes with a null
  36. // function. These will be used when we can prove (through pointer analysis)
  37. // that an indirect call site can call only a specific set of functions.
  38. //
  39. // Because of these properties, the CallGraph captures a conservative superset
  40. // of all of the caller-callee relationships, which is useful for
  41. // transformations.
  42. //
  43. // The CallGraph class also attempts to figure out what the root of the
  44. // CallGraph is, which it currently does by looking for a function named 'main'.
  45. // If no function named 'main' is found, the external node is used as the entry
  46. // node, reflecting the fact that any function without internal linkage could
  47. // be called into (which is common for libraries).
  48. //
  49. //===----------------------------------------------------------------------===//
  50. #ifndef LLVM_ANALYSIS_CALLGRAPH_H
  51. #define LLVM_ANALYSIS_CALLGRAPH_H
  52. #include "llvm/ADT/GraphTraits.h"
  53. #include "llvm/ADT/STLExtras.h"
  54. #include "llvm/IR/Function.h"
  55. #include "llvm/Pass.h"
  56. #include "llvm/Support/CallSite.h"
  57. #include "llvm/Support/IncludeFile.h"
  58. #include "llvm/Support/ValueHandle.h"
  59. #include <map>
  60. namespace llvm {
  61. class Function;
  62. class Module;
  63. class CallGraphNode;
  64. //===----------------------------------------------------------------------===//
  65. // CallGraph class definition
  66. //
  67. class CallGraph : public ModulePass {
  68. Module *Mod; // The module this call graph represents
  69. typedef std::map<const Function *, CallGraphNode *> FunctionMapTy;
  70. FunctionMapTy FunctionMap; // Map from a function to its node
  71. // Root is root of the call graph, or the external node if a 'main' function
  72. // couldn't be found.
  73. //
  74. CallGraphNode *Root;
  75. // ExternalCallingNode - This node has edges to all external functions and
  76. // those internal functions that have their address taken.
  77. CallGraphNode *ExternalCallingNode;
  78. // CallsExternalNode - This node has edges to it from all functions making
  79. // indirect calls or calling an external function.
  80. CallGraphNode *CallsExternalNode;
  81. /// Replace the function represented by this node by another.
  82. /// This does not rescan the body of the function, so it is suitable when
  83. /// splicing the body of one function to another while also updating all
  84. /// callers from the old function to the new.
  85. ///
  86. void spliceFunction(const Function *From, const Function *To);
  87. // Add a function to the call graph, and link the node to all of the functions
  88. // that it calls.
  89. void addToCallGraph(Function *F);
  90. public:
  91. static char ID; // Class identification, replacement for typeinfo
  92. //===---------------------------------------------------------------------
  93. // Accessors.
  94. //
  95. typedef FunctionMapTy::iterator iterator;
  96. typedef FunctionMapTy::const_iterator const_iterator;
  97. /// getModule - Return the module the call graph corresponds to.
  98. ///
  99. Module &getModule() const { return *Mod; }
  100. inline iterator begin() { return FunctionMap.begin(); }
  101. inline iterator end() { return FunctionMap.end(); }
  102. inline const_iterator begin() const { return FunctionMap.begin(); }
  103. inline const_iterator end() const { return FunctionMap.end(); }
  104. // Subscripting operators, return the call graph node for the provided
  105. // function
  106. inline const CallGraphNode *operator[](const Function *F) const {
  107. const_iterator I = FunctionMap.find(F);
  108. assert(I != FunctionMap.end() && "Function not in callgraph!");
  109. return I->second;
  110. }
  111. inline CallGraphNode *operator[](const Function *F) {
  112. const_iterator I = FunctionMap.find(F);
  113. assert(I != FunctionMap.end() && "Function not in callgraph!");
  114. return I->second;
  115. }
  116. /// Returns the CallGraphNode which is used to represent undetermined calls
  117. /// into the callgraph.
  118. CallGraphNode *getExternalCallingNode() const { return ExternalCallingNode; }
  119. CallGraphNode *getCallsExternalNode() const { return CallsExternalNode; }
  120. /// Return the root/main method in the module, or some other root node, such
  121. /// as the externalcallingnode.
  122. CallGraphNode *getRoot() { return Root; }
  123. const CallGraphNode *getRoot() const { return Root; }
  124. //===---------------------------------------------------------------------
  125. // Functions to keep a call graph up to date with a function that has been
  126. // modified.
  127. //
  128. /// removeFunctionFromModule - Unlink the function from this module, returning
  129. /// it. Because this removes the function from the module, the call graph
  130. /// node is destroyed. This is only valid if the function does not call any
  131. /// other functions (ie, there are no edges in it's CGN). The easiest way to
  132. /// do this is to dropAllReferences before calling this.
  133. ///
  134. Function *removeFunctionFromModule(CallGraphNode *CGN);
  135. /// getOrInsertFunction - This method is identical to calling operator[], but
  136. /// it will insert a new CallGraphNode for the specified function if one does
  137. /// not already exist.
  138. CallGraphNode *getOrInsertFunction(const Function *F);
  139. CallGraph();
  140. virtual ~CallGraph() { releaseMemory(); }
  141. virtual void getAnalysisUsage(AnalysisUsage &AU) const;
  142. virtual bool runOnModule(Module &M);
  143. virtual void releaseMemory();
  144. void print(raw_ostream &o, const Module *) const;
  145. void dump() const;
  146. };
  147. //===----------------------------------------------------------------------===//
  148. // CallGraphNode class definition.
  149. //
  150. class CallGraphNode {
  151. friend class CallGraph;
  152. AssertingVH<Function> F;
  153. // CallRecord - This is a pair of the calling instruction (a call or invoke)
  154. // and the callgraph node being called.
  155. public:
  156. typedef std::pair<WeakVH, CallGraphNode*> CallRecord;
  157. private:
  158. std::vector<CallRecord> CalledFunctions;
  159. /// NumReferences - This is the number of times that this CallGraphNode occurs
  160. /// in the CalledFunctions array of this or other CallGraphNodes.
  161. unsigned NumReferences;
  162. CallGraphNode(const CallGraphNode &) LLVM_DELETED_FUNCTION;
  163. void operator=(const CallGraphNode &) LLVM_DELETED_FUNCTION;
  164. void DropRef() { --NumReferences; }
  165. void AddRef() { ++NumReferences; }
  166. public:
  167. typedef std::vector<CallRecord> CalledFunctionsVector;
  168. // CallGraphNode ctor - Create a node for the specified function.
  169. inline CallGraphNode(Function *f) : F(f), NumReferences(0) {}
  170. ~CallGraphNode() {
  171. assert(NumReferences == 0 && "Node deleted while references remain");
  172. }
  173. //===---------------------------------------------------------------------
  174. // Accessor methods.
  175. //
  176. typedef std::vector<CallRecord>::iterator iterator;
  177. typedef std::vector<CallRecord>::const_iterator const_iterator;
  178. // getFunction - Return the function that this call graph node represents.
  179. Function *getFunction() const { return F; }
  180. inline iterator begin() { return CalledFunctions.begin(); }
  181. inline iterator end() { return CalledFunctions.end(); }
  182. inline const_iterator begin() const { return CalledFunctions.begin(); }
  183. inline const_iterator end() const { return CalledFunctions.end(); }
  184. inline bool empty() const { return CalledFunctions.empty(); }
  185. inline unsigned size() const { return (unsigned)CalledFunctions.size(); }
  186. /// getNumReferences - Return the number of other CallGraphNodes in this
  187. /// CallGraph that reference this node in their callee list.
  188. unsigned getNumReferences() const { return NumReferences; }
  189. // Subscripting operator - Return the i'th called function.
  190. //
  191. CallGraphNode *operator[](unsigned i) const {
  192. assert(i < CalledFunctions.size() && "Invalid index");
  193. return CalledFunctions[i].second;
  194. }
  195. /// dump - Print out this call graph node.
  196. ///
  197. void dump() const;
  198. void print(raw_ostream &OS) const;
  199. //===---------------------------------------------------------------------
  200. // Methods to keep a call graph up to date with a function that has been
  201. // modified
  202. //
  203. /// removeAllCalledFunctions - As the name implies, this removes all edges
  204. /// from this CallGraphNode to any functions it calls.
  205. void removeAllCalledFunctions() {
  206. while (!CalledFunctions.empty()) {
  207. CalledFunctions.back().second->DropRef();
  208. CalledFunctions.pop_back();
  209. }
  210. }
  211. /// stealCalledFunctionsFrom - Move all the callee information from N to this
  212. /// node.
  213. void stealCalledFunctionsFrom(CallGraphNode *N) {
  214. assert(CalledFunctions.empty() &&
  215. "Cannot steal callsite information if I already have some");
  216. std::swap(CalledFunctions, N->CalledFunctions);
  217. }
  218. /// addCalledFunction - Add a function to the list of functions called by this
  219. /// one.
  220. void addCalledFunction(CallSite CS, CallGraphNode *M) {
  221. assert(!CS.getInstruction() ||
  222. !CS.getCalledFunction() ||
  223. !CS.getCalledFunction()->isIntrinsic());
  224. CalledFunctions.push_back(std::make_pair(CS.getInstruction(), M));
  225. M->AddRef();
  226. }
  227. void removeCallEdge(iterator I) {
  228. I->second->DropRef();
  229. *I = CalledFunctions.back();
  230. CalledFunctions.pop_back();
  231. }
  232. /// removeCallEdgeFor - This method removes the edge in the node for the
  233. /// specified call site. Note that this method takes linear time, so it
  234. /// should be used sparingly.
  235. void removeCallEdgeFor(CallSite CS);
  236. /// removeAnyCallEdgeTo - This method removes all call edges from this node
  237. /// to the specified callee function. This takes more time to execute than
  238. /// removeCallEdgeTo, so it should not be used unless necessary.
  239. void removeAnyCallEdgeTo(CallGraphNode *Callee);
  240. /// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite
  241. /// from this node to the specified callee function.
  242. void removeOneAbstractEdgeTo(CallGraphNode *Callee);
  243. /// replaceCallEdge - This method replaces the edge in the node for the
  244. /// specified call site with a new one. Note that this method takes linear
  245. /// time, so it should be used sparingly.
  246. void replaceCallEdge(CallSite CS, CallSite NewCS, CallGraphNode *NewNode);
  247. /// allReferencesDropped - This is a special function that should only be
  248. /// used by the CallGraph class.
  249. void allReferencesDropped() {
  250. NumReferences = 0;
  251. }
  252. };
  253. //===----------------------------------------------------------------------===//
  254. // GraphTraits specializations for call graphs so that they can be treated as
  255. // graphs by the generic graph algorithms.
  256. //
  257. // Provide graph traits for tranversing call graphs using standard graph
  258. // traversals.
  259. template <> struct GraphTraits<CallGraphNode*> {
  260. typedef CallGraphNode NodeType;
  261. typedef CallGraphNode::CallRecord CGNPairTy;
  262. typedef std::pointer_to_unary_function<CGNPairTy, CallGraphNode*> CGNDerefFun;
  263. static NodeType *getEntryNode(CallGraphNode *CGN) { return CGN; }
  264. typedef mapped_iterator<NodeType::iterator, CGNDerefFun> ChildIteratorType;
  265. static inline ChildIteratorType child_begin(NodeType *N) {
  266. return map_iterator(N->begin(), CGNDerefFun(CGNDeref));
  267. }
  268. static inline ChildIteratorType child_end (NodeType *N) {
  269. return map_iterator(N->end(), CGNDerefFun(CGNDeref));
  270. }
  271. static CallGraphNode *CGNDeref(CGNPairTy P) {
  272. return P.second;
  273. }
  274. };
  275. template <> struct GraphTraits<const CallGraphNode*> {
  276. typedef const CallGraphNode NodeType;
  277. typedef NodeType::const_iterator ChildIteratorType;
  278. static NodeType *getEntryNode(const CallGraphNode *CGN) { return CGN; }
  279. static inline ChildIteratorType child_begin(NodeType *N) { return N->begin();}
  280. static inline ChildIteratorType child_end (NodeType *N) { return N->end(); }
  281. };
  282. template<> struct GraphTraits<CallGraph*> : public GraphTraits<CallGraphNode*> {
  283. static NodeType *getEntryNode(CallGraph *CGN) {
  284. return CGN->getExternalCallingNode(); // Start at the external node!
  285. }
  286. typedef std::pair<const Function*, CallGraphNode*> PairTy;
  287. typedef std::pointer_to_unary_function<PairTy, CallGraphNode&> DerefFun;
  288. // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
  289. typedef mapped_iterator<CallGraph::iterator, DerefFun> nodes_iterator;
  290. static nodes_iterator nodes_begin(CallGraph *CG) {
  291. return map_iterator(CG->begin(), DerefFun(CGdereference));
  292. }
  293. static nodes_iterator nodes_end (CallGraph *CG) {
  294. return map_iterator(CG->end(), DerefFun(CGdereference));
  295. }
  296. static CallGraphNode &CGdereference(PairTy P) {
  297. return *P.second;
  298. }
  299. };
  300. template<> struct GraphTraits<const CallGraph*> :
  301. public GraphTraits<const CallGraphNode*> {
  302. static NodeType *getEntryNode(const CallGraph *CGN) {
  303. return CGN->getExternalCallingNode();
  304. }
  305. // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
  306. typedef CallGraph::const_iterator nodes_iterator;
  307. static nodes_iterator nodes_begin(const CallGraph *CG) { return CG->begin(); }
  308. static nodes_iterator nodes_end (const CallGraph *CG) { return CG->end(); }
  309. };
  310. } // End llvm namespace
  311. // Make sure that any clients of this file link in CallGraph.cpp
  312. FORCE_DEFINING_FILE_TO_BE_LINKED(CallGraph)
  313. #endif