Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

378 lines
14 KiB

  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 {
  68. protected:
  69. Module *Mod; // The module this call graph represents
  70. typedef std::map<const Function *, CallGraphNode *> FunctionMapTy;
  71. FunctionMapTy FunctionMap; // Map from a function to its node
  72. public:
  73. static char ID; // Class identification, replacement for typeinfo
  74. //===---------------------------------------------------------------------
  75. // Accessors.
  76. //
  77. typedef FunctionMapTy::iterator iterator;
  78. typedef FunctionMapTy::const_iterator const_iterator;
  79. /// getModule - Return the module the call graph corresponds to.
  80. ///
  81. Module &getModule() const { return *Mod; }
  82. inline iterator begin() { return FunctionMap.begin(); }
  83. inline iterator end() { return FunctionMap.end(); }
  84. inline const_iterator begin() const { return FunctionMap.begin(); }
  85. inline const_iterator end() const { return FunctionMap.end(); }
  86. // Subscripting operators, return the call graph node for the provided
  87. // function
  88. inline const CallGraphNode *operator[](const Function *F) const {
  89. const_iterator I = FunctionMap.find(F);
  90. assert(I != FunctionMap.end() && "Function not in callgraph!");
  91. return I->second;
  92. }
  93. inline CallGraphNode *operator[](const Function *F) {
  94. const_iterator I = FunctionMap.find(F);
  95. assert(I != FunctionMap.end() && "Function not in callgraph!");
  96. return I->second;
  97. }
  98. /// Returns the CallGraphNode which is used to represent undetermined calls
  99. /// into the callgraph. Override this if you want behavioral inheritance.
  100. virtual CallGraphNode* getExternalCallingNode() const { return 0; }
  101. virtual CallGraphNode* getCallsExternalNode() const { return 0; }
  102. /// Return the root/main method in the module, or some other root node, such
  103. /// as the externalcallingnode. Overload these if you behavioral
  104. /// inheritance.
  105. virtual CallGraphNode* getRoot() { return 0; }
  106. virtual const CallGraphNode* getRoot() const { return 0; }
  107. //===---------------------------------------------------------------------
  108. // Functions to keep a call graph up to date with a function that has been
  109. // modified.
  110. //
  111. /// removeFunctionFromModule - Unlink the function from this module, returning
  112. /// it. Because this removes the function from the module, the call graph
  113. /// node is destroyed. This is only valid if the function does not call any
  114. /// other functions (ie, there are no edges in it's CGN). The easiest way to
  115. /// do this is to dropAllReferences before calling this.
  116. ///
  117. Function *removeFunctionFromModule(CallGraphNode *CGN);
  118. Function *removeFunctionFromModule(Function *F) {
  119. return removeFunctionFromModule((*this)[F]);
  120. }
  121. /// getOrInsertFunction - This method is identical to calling operator[], but
  122. /// it will insert a new CallGraphNode for the specified function if one does
  123. /// not already exist.
  124. CallGraphNode *getOrInsertFunction(const Function *F);
  125. /// spliceFunction - Replace the function represented by this node by another.
  126. /// This does not rescan the body of the function, so it is suitable when
  127. /// splicing the body of one function to another while also updating all
  128. /// callers from the old function to the new.
  129. ///
  130. void spliceFunction(const Function *From, const Function *To);
  131. //===---------------------------------------------------------------------
  132. // Pass infrastructure interface glue code.
  133. //
  134. protected:
  135. CallGraph() {}
  136. public:
  137. virtual ~CallGraph() { destroy(); }
  138. /// initialize - Call this method before calling other methods,
  139. /// re/initializes the state of the CallGraph.
  140. ///
  141. void initialize(Module &M);
  142. void print(raw_ostream &o, Module *) const;
  143. void dump() const;
  144. protected:
  145. // destroy - Release memory for the call graph
  146. virtual void destroy();
  147. };
  148. //===----------------------------------------------------------------------===//
  149. // CallGraphNode class definition.
  150. //
  151. class CallGraphNode {
  152. friend class CallGraph;
  153. AssertingVH<Function> F;
  154. // CallRecord - This is a pair of the calling instruction (a call or invoke)
  155. // and the callgraph node being called.
  156. public:
  157. typedef std::pair<WeakVH, CallGraphNode*> CallRecord;
  158. private:
  159. std::vector<CallRecord> CalledFunctions;
  160. /// NumReferences - This is the number of times that this CallGraphNode occurs
  161. /// in the CalledFunctions array of this or other CallGraphNodes.
  162. unsigned NumReferences;
  163. CallGraphNode(const CallGraphNode &) LLVM_DELETED_FUNCTION;
  164. void operator=(const CallGraphNode &) LLVM_DELETED_FUNCTION;
  165. void DropRef() { --NumReferences; }
  166. void AddRef() { ++NumReferences; }
  167. public:
  168. typedef std::vector<CallRecord> CalledFunctionsVector;
  169. // CallGraphNode ctor - Create a node for the specified function.
  170. inline CallGraphNode(Function *f) : F(f), NumReferences(0) {}
  171. ~CallGraphNode() {
  172. assert(NumReferences == 0 && "Node deleted while references remain");
  173. }
  174. //===---------------------------------------------------------------------
  175. // Accessor methods.
  176. //
  177. typedef std::vector<CallRecord>::iterator iterator;
  178. typedef std::vector<CallRecord>::const_iterator const_iterator;
  179. // getFunction - Return the function that this call graph node represents.
  180. Function *getFunction() const { return F; }
  181. inline iterator begin() { return CalledFunctions.begin(); }
  182. inline iterator end() { return CalledFunctions.end(); }
  183. inline const_iterator begin() const { return CalledFunctions.begin(); }
  184. inline const_iterator end() const { return CalledFunctions.end(); }
  185. inline bool empty() const { return CalledFunctions.empty(); }
  186. inline unsigned size() const { return (unsigned)CalledFunctions.size(); }
  187. /// getNumReferences - Return the number of other CallGraphNodes in this
  188. /// CallGraph that reference this node in their callee list.
  189. unsigned getNumReferences() const { return NumReferences; }
  190. // Subscripting operator - Return the i'th called function.
  191. //
  192. CallGraphNode *operator[](unsigned i) const {
  193. assert(i < CalledFunctions.size() && "Invalid index");
  194. return CalledFunctions[i].second;
  195. }
  196. /// dump - Print out this call graph node.
  197. ///
  198. void dump() const;
  199. void print(raw_ostream &OS) const;
  200. //===---------------------------------------------------------------------
  201. // Methods to keep a call graph up to date with a function that has been
  202. // modified
  203. //
  204. /// removeAllCalledFunctions - As the name implies, this removes all edges
  205. /// from this CallGraphNode to any functions it calls.
  206. void removeAllCalledFunctions() {
  207. while (!CalledFunctions.empty()) {
  208. CalledFunctions.back().second->DropRef();
  209. CalledFunctions.pop_back();
  210. }
  211. }
  212. /// stealCalledFunctionsFrom - Move all the callee information from N to this
  213. /// node.
  214. void stealCalledFunctionsFrom(CallGraphNode *N) {
  215. assert(CalledFunctions.empty() &&
  216. "Cannot steal callsite information if I already have some");
  217. std::swap(CalledFunctions, N->CalledFunctions);
  218. }
  219. /// addCalledFunction - Add a function to the list of functions called by this
  220. /// one.
  221. void addCalledFunction(CallSite CS, CallGraphNode *M) {
  222. assert(!CS.getInstruction() ||
  223. !CS.getCalledFunction() ||
  224. !CS.getCalledFunction()->isIntrinsic());
  225. CalledFunctions.push_back(std::make_pair(CS.getInstruction(), M));
  226. M->AddRef();
  227. }
  228. void removeCallEdge(iterator I) {
  229. I->second->DropRef();
  230. *I = CalledFunctions.back();
  231. CalledFunctions.pop_back();
  232. }
  233. /// removeCallEdgeFor - This method removes the edge in the node for the
  234. /// specified call site. Note that this method takes linear time, so it
  235. /// should be used sparingly.
  236. void removeCallEdgeFor(CallSite CS);
  237. /// removeAnyCallEdgeTo - This method removes all call edges from this node
  238. /// to the specified callee function. This takes more time to execute than
  239. /// removeCallEdgeTo, so it should not be used unless necessary.
  240. void removeAnyCallEdgeTo(CallGraphNode *Callee);
  241. /// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite
  242. /// from this node to the specified callee function.
  243. void removeOneAbstractEdgeTo(CallGraphNode *Callee);
  244. /// replaceCallEdge - This method replaces the edge in the node for the
  245. /// specified call site with a new one. Note that this method takes linear
  246. /// time, so it should be used sparingly.
  247. void replaceCallEdge(CallSite CS, CallSite NewCS, CallGraphNode *NewNode);
  248. /// allReferencesDropped - This is a special function that should only be
  249. /// used by the CallGraph class.
  250. void allReferencesDropped() {
  251. NumReferences = 0;
  252. }
  253. };
  254. //===----------------------------------------------------------------------===//
  255. // GraphTraits specializations for call graphs so that they can be treated as
  256. // graphs by the generic graph algorithms.
  257. //
  258. // Provide graph traits for tranversing call graphs using standard graph
  259. // traversals.
  260. template <> struct GraphTraits<CallGraphNode*> {
  261. typedef CallGraphNode NodeType;
  262. typedef CallGraphNode::CallRecord CGNPairTy;
  263. typedef std::pointer_to_unary_function<CGNPairTy, CallGraphNode*> CGNDerefFun;
  264. static NodeType *getEntryNode(CallGraphNode *CGN) { return CGN; }
  265. typedef mapped_iterator<NodeType::iterator, CGNDerefFun> ChildIteratorType;
  266. static inline ChildIteratorType child_begin(NodeType *N) {
  267. return map_iterator(N->begin(), CGNDerefFun(CGNDeref));
  268. }
  269. static inline ChildIteratorType child_end (NodeType *N) {
  270. return map_iterator(N->end(), CGNDerefFun(CGNDeref));
  271. }
  272. static CallGraphNode *CGNDeref(CGNPairTy P) {
  273. return P.second;
  274. }
  275. };
  276. template <> struct GraphTraits<const CallGraphNode*> {
  277. typedef const CallGraphNode NodeType;
  278. typedef NodeType::const_iterator ChildIteratorType;
  279. static NodeType *getEntryNode(const CallGraphNode *CGN) { return CGN; }
  280. static inline ChildIteratorType child_begin(NodeType *N) { return N->begin();}
  281. static inline ChildIteratorType child_end (NodeType *N) { return N->end(); }
  282. };
  283. template<> struct GraphTraits<CallGraph*> : public GraphTraits<CallGraphNode*> {
  284. static NodeType *getEntryNode(CallGraph *CGN) {
  285. return CGN->getExternalCallingNode(); // Start at the external node!
  286. }
  287. typedef std::pair<const Function*, CallGraphNode*> PairTy;
  288. typedef std::pointer_to_unary_function<PairTy, CallGraphNode&> DerefFun;
  289. // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
  290. typedef mapped_iterator<CallGraph::iterator, DerefFun> nodes_iterator;
  291. static nodes_iterator nodes_begin(CallGraph *CG) {
  292. return map_iterator(CG->begin(), DerefFun(CGdereference));
  293. }
  294. static nodes_iterator nodes_end (CallGraph *CG) {
  295. return map_iterator(CG->end(), DerefFun(CGdereference));
  296. }
  297. static CallGraphNode &CGdereference(PairTy P) {
  298. return *P.second;
  299. }
  300. };
  301. template<> struct GraphTraits<const CallGraph*> :
  302. public GraphTraits<const CallGraphNode*> {
  303. static NodeType *getEntryNode(const CallGraph *CGN) {
  304. return CGN->getExternalCallingNode();
  305. }
  306. // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
  307. typedef CallGraph::const_iterator nodes_iterator;
  308. static nodes_iterator nodes_begin(const CallGraph *CG) { return CG->begin(); }
  309. static nodes_iterator nodes_end (const CallGraph *CG) { return CG->end(); }
  310. };
  311. } // End llvm namespace
  312. // Make sure that any clients of this file link in CallGraph.cpp
  313. FORCE_DEFINING_FILE_TO_BE_LINKED(CallGraph)
  314. #endif