CallGraph.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. /// \file
  10. ///
  11. /// This file provides interfaces used to build and manipulate a call graph,
  12. /// which is a very useful tool for interprocedural optimization.
  13. ///
  14. /// Every function in a module is represented as a node in the call graph. The
  15. /// callgraph node keeps track of which functions are called by the function
  16. /// corresponding to the node.
  17. ///
  18. /// A call graph may contain nodes where the function that they correspond to
  19. /// is null. These 'external' nodes are used to represent control flow that is
  20. /// not represented (or analyzable) in the module. In particular, this
  21. /// analysis builds one external node such that:
  22. /// 1. All functions in the module without internal linkage will have edges
  23. /// from this external node, indicating that they could be called by
  24. /// functions outside of the module.
  25. /// 2. All functions whose address is used for something more than a direct
  26. /// call, for example being stored into a memory location will also have
  27. /// an edge from this external node. Since they may be called by an
  28. /// unknown caller later, they must be tracked as such.
  29. ///
  30. /// There is a second external node added for calls that leave this module.
  31. /// Functions have a call edge to the external node iff:
  32. /// 1. The function is external, reflecting the fact that they could call
  33. /// anything without internal linkage or that has its address taken.
  34. /// 2. The function contains an indirect function call.
  35. ///
  36. /// As an extension in the future, there may be multiple nodes with a null
  37. /// function. These will be used when we can prove (through pointer analysis)
  38. /// that an indirect call site can call only a specific set of functions.
  39. ///
  40. /// Because of these properties, the CallGraph captures a conservative superset
  41. /// of all of the caller-callee relationships, which is useful for
  42. /// transformations.
  43. ///
  44. /// The CallGraph class also attempts to figure out what the root of the
  45. /// CallGraph is, which it currently does by looking for a function named
  46. /// 'main'. If no function named 'main' is found, the external node is used as
  47. /// the entry node, reflecting the fact that any function without internal
  48. /// linkage could be called into (which is common for libraries).
  49. ///
  50. //===----------------------------------------------------------------------===//
  51. #ifndef LLVM_ANALYSIS_CALLGRAPH_H
  52. #define LLVM_ANALYSIS_CALLGRAPH_H
  53. #include "llvm/ADT/GraphTraits.h"
  54. #include "llvm/ADT/STLExtras.h"
  55. #include "llvm/IR/CallSite.h"
  56. #include "llvm/IR/Function.h"
  57. #include "llvm/IR/Intrinsics.h"
  58. #include "llvm/IR/ValueHandle.h"
  59. #include "llvm/Pass.h"
  60. #include <map>
  61. namespace llvm {
  62. class Function;
  63. class Module;
  64. class CallGraphNode;
  65. /// \brief The basic data container for the call graph of a \c Module of IR.
  66. ///
  67. /// This class exposes both the interface to the call graph for a module of IR.
  68. ///
  69. /// The core call graph itself can also be updated to reflect changes to the IR.
  70. class CallGraph {
  71. Module &M;
  72. typedef std::map<const Function *, CallGraphNode *> FunctionMapTy;
  73. /// \brief A map from \c Function* to \c CallGraphNode*.
  74. FunctionMapTy FunctionMap;
  75. /// \brief Root is root of the call graph, or the external node if a 'main'
  76. /// function couldn't be found.
  77. CallGraphNode *Root;
  78. /// \brief This node has edges to all external functions and those internal
  79. /// functions that have their address taken.
  80. CallGraphNode *ExternalCallingNode;
  81. /// \brief This node has edges to it from all functions making indirect calls
  82. /// or calling an external function.
  83. CallGraphNode *CallsExternalNode;
  84. /// \brief Replace the function represented by this node by another.
  85. ///
  86. /// This does not rescan the body of the function, so it is suitable when
  87. /// splicing the body of one function to another while also updating all
  88. /// callers from the old function to the new.
  89. void spliceFunction(const Function *From, const Function *To);
  90. /// \brief Add a function to the call graph, and link the node to all of the
  91. /// functions that it calls.
  92. void addToCallGraph(Function *F);
  93. void reset(); // HLSL Change
  94. public:
  95. CallGraph(Module &M);
  96. ~CallGraph();
  97. void print(raw_ostream &OS) const;
  98. void dump() const;
  99. typedef FunctionMapTy::iterator iterator;
  100. typedef FunctionMapTy::const_iterator const_iterator;
  101. /// \brief Returns the module the call graph corresponds to.
  102. Module &getModule() const { return M; }
  103. inline iterator begin() { return FunctionMap.begin(); }
  104. inline iterator end() { return FunctionMap.end(); }
  105. inline const_iterator begin() const { return FunctionMap.begin(); }
  106. inline const_iterator end() const { return FunctionMap.end(); }
  107. /// \brief Returns the call graph node for the provided function.
  108. inline const CallGraphNode *operator[](const Function *F) const {
  109. const_iterator I = FunctionMap.find(F);
  110. assert(I != FunctionMap.end() && "Function not in callgraph!");
  111. return I->second;
  112. }
  113. /// \brief Returns the call graph node for the provided function.
  114. inline CallGraphNode *operator[](const Function *F) {
  115. const_iterator I = FunctionMap.find(F);
  116. assert(I != FunctionMap.end() && "Function not in callgraph!");
  117. return I->second;
  118. }
  119. /// \brief Returns the \c CallGraphNode which is used to represent
  120. /// undetermined calls into the callgraph.
  121. CallGraphNode *getExternalCallingNode() const { return ExternalCallingNode; }
  122. CallGraphNode *getCallsExternalNode() const { return CallsExternalNode; }
  123. //===---------------------------------------------------------------------
  124. // Functions to keep a call graph up to date with a function that has been
  125. // modified.
  126. //
  127. /// \brief Unlink the function from this module, returning it.
  128. ///
  129. /// Because this removes the function from the module, the call graph node is
  130. /// destroyed. This is only valid if the function does not call any other
  131. /// functions (ie, there are no edges in it's CGN). The easiest way to do
  132. /// this is to dropAllReferences before calling this.
  133. Function *removeFunctionFromModule(CallGraphNode *CGN);
  134. /// \brief Similar to operator[], but this will insert a new CallGraphNode for
  135. /// \c F if one does not already exist.
  136. CallGraphNode *getOrInsertFunction(const Function *F);
  137. };
  138. /// \brief A node in the call graph for a module.
  139. ///
  140. /// Typically represents a function in the call graph. There are also special
  141. /// "null" nodes used to represent theoretical entries in the call graph.
  142. class CallGraphNode {
  143. public:
  144. /// \brief A pair of the calling instruction (a call or invoke)
  145. /// and the call graph node being called.
  146. typedef std::pair<WeakVH, CallGraphNode *> CallRecord;
  147. public:
  148. typedef std::vector<CallRecord> CalledFunctionsVector;
  149. /// \brief Creates a node for the specified function.
  150. inline CallGraphNode(Function *F) : F(F), NumReferences(0) {}
  151. ~CallGraphNode() {
  152. assert(NumReferences == 0 && "Node deleted while references remain");
  153. }
  154. typedef std::vector<CallRecord>::iterator iterator;
  155. typedef std::vector<CallRecord>::const_iterator const_iterator;
  156. /// \brief Returns the function that this call graph node represents.
  157. Function *getFunction() const { return F; }
  158. inline iterator begin() { return CalledFunctions.begin(); }
  159. inline iterator end() { return CalledFunctions.end(); }
  160. inline const_iterator begin() const { return CalledFunctions.begin(); }
  161. inline const_iterator end() const { return CalledFunctions.end(); }
  162. inline bool empty() const { return CalledFunctions.empty(); }
  163. inline unsigned size() const { return (unsigned)CalledFunctions.size(); }
  164. /// \brief Returns the number of other CallGraphNodes in this CallGraph that
  165. /// reference this node in their callee list.
  166. unsigned getNumReferences() const { return NumReferences; }
  167. /// \brief Returns the i'th called function.
  168. CallGraphNode *operator[](unsigned i) const {
  169. assert(i < CalledFunctions.size() && "Invalid index");
  170. return CalledFunctions[i].second;
  171. }
  172. /// \brief Print out this call graph node.
  173. void dump() const;
  174. void print(raw_ostream &OS) const;
  175. //===---------------------------------------------------------------------
  176. // Methods to keep a call graph up to date with a function that has been
  177. // modified
  178. //
  179. /// \brief Removes all edges from this CallGraphNode to any functions it
  180. /// calls.
  181. void removeAllCalledFunctions() {
  182. while (!CalledFunctions.empty()) {
  183. CalledFunctions.back().second->DropRef();
  184. CalledFunctions.pop_back();
  185. }
  186. }
  187. /// \brief Moves all the callee information from N to this node.
  188. void stealCalledFunctionsFrom(CallGraphNode *N) {
  189. assert(CalledFunctions.empty() &&
  190. "Cannot steal callsite information if I already have some");
  191. std::swap(CalledFunctions, N->CalledFunctions);
  192. }
  193. /// \brief Adds a function to the list of functions called by this one.
  194. void addCalledFunction(CallSite CS, CallGraphNode *M) {
  195. assert(!CS.getInstruction() || !CS.getCalledFunction() ||
  196. !CS.getCalledFunction()->isIntrinsic() ||
  197. !Intrinsic::isLeaf(CS.getCalledFunction()->getIntrinsicID()));
  198. CalledFunctions.emplace_back(CS.getInstruction(), M);
  199. M->AddRef();
  200. }
  201. void removeCallEdge(iterator I) {
  202. I->second->DropRef();
  203. *I = CalledFunctions.back();
  204. CalledFunctions.pop_back();
  205. }
  206. /// \brief Removes the edge in the node for the specified call site.
  207. ///
  208. /// Note that this method takes linear time, so it should be used sparingly.
  209. void removeCallEdgeFor(CallSite CS);
  210. /// \brief Removes all call edges from this node to the specified callee
  211. /// function.
  212. ///
  213. /// This takes more time to execute than removeCallEdgeTo, so it should not
  214. /// be used unless necessary.
  215. void removeAnyCallEdgeTo(CallGraphNode *Callee);
  216. /// \brief Removes one edge associated with a null callsite from this node to
  217. /// the specified callee function.
  218. void removeOneAbstractEdgeTo(CallGraphNode *Callee);
  219. /// \brief Replaces the edge in the node for the specified call site with a
  220. /// new one.
  221. ///
  222. /// Note that this method takes linear time, so it should be used sparingly.
  223. void replaceCallEdge(CallSite CS, CallSite NewCS, CallGraphNode *NewNode);
  224. private:
  225. friend class CallGraph;
  226. AssertingVH<Function> F;
  227. std::vector<CallRecord> CalledFunctions;
  228. /// \brief The number of times that this CallGraphNode occurs in the
  229. /// CalledFunctions array of this or other CallGraphNodes.
  230. unsigned NumReferences;
  231. CallGraphNode(const CallGraphNode &) = delete;
  232. void operator=(const CallGraphNode &) = delete;
  233. void DropRef() { --NumReferences; }
  234. void AddRef() { ++NumReferences; }
  235. /// \brief A special function that should only be used by the CallGraph class.
  236. void allReferencesDropped() { NumReferences = 0; }
  237. };
  238. /// \brief An analysis pass to compute the \c CallGraph for a \c Module.
  239. ///
  240. /// This class implements the concept of an analysis pass used by the \c
  241. /// ModuleAnalysisManager to run an analysis over a module and cache the
  242. /// resulting data.
  243. class CallGraphAnalysis {
  244. public:
  245. /// \brief A formulaic typedef to inform clients of the result type.
  246. typedef CallGraph Result;
  247. static void *ID() { return (void *)&PassID; }
  248. /// \brief Compute the \c CallGraph for the module \c M.
  249. ///
  250. /// The real work here is done in the \c CallGraph constructor.
  251. CallGraph run(Module *M) { return CallGraph(*M); }
  252. private:
  253. static char PassID;
  254. };
  255. /// \brief The \c ModulePass which wraps up a \c CallGraph and the logic to
  256. /// build it.
  257. ///
  258. /// This class exposes both the interface to the call graph container and the
  259. /// module pass which runs over a module of IR and produces the call graph. The
  260. /// call graph interface is entirelly a wrapper around a \c CallGraph object
  261. /// which is stored internally for each module.
  262. class CallGraphWrapperPass : public ModulePass {
  263. std::unique_ptr<CallGraph> G;
  264. public:
  265. static char ID; // Class identification, replacement for typeinfo
  266. CallGraphWrapperPass();
  267. ~CallGraphWrapperPass() override;
  268. /// \brief The internal \c CallGraph around which the rest of this interface
  269. /// is wrapped.
  270. const CallGraph &getCallGraph() const { return *G; }
  271. CallGraph &getCallGraph() { return *G; }
  272. typedef CallGraph::iterator iterator;
  273. typedef CallGraph::const_iterator const_iterator;
  274. /// \brief Returns the module the call graph corresponds to.
  275. Module &getModule() const { return G->getModule(); }
  276. inline iterator begin() { return G->begin(); }
  277. inline iterator end() { return G->end(); }
  278. inline const_iterator begin() const { return G->begin(); }
  279. inline const_iterator end() const { return G->end(); }
  280. /// \brief Returns the call graph node for the provided function.
  281. inline const CallGraphNode *operator[](const Function *F) const {
  282. return (*G)[F];
  283. }
  284. /// \brief Returns the call graph node for the provided function.
  285. inline CallGraphNode *operator[](const Function *F) { return (*G)[F]; }
  286. /// \brief Returns the \c CallGraphNode which is used to represent
  287. /// undetermined calls into the callgraph.
  288. CallGraphNode *getExternalCallingNode() const {
  289. return G->getExternalCallingNode();
  290. }
  291. CallGraphNode *getCallsExternalNode() const {
  292. return G->getCallsExternalNode();
  293. }
  294. //===---------------------------------------------------------------------
  295. // Functions to keep a call graph up to date with a function that has been
  296. // modified.
  297. //
  298. /// \brief Unlink the function from this module, returning it.
  299. ///
  300. /// Because this removes the function from the module, the call graph node is
  301. /// destroyed. This is only valid if the function does not call any other
  302. /// functions (ie, there are no edges in it's CGN). The easiest way to do
  303. /// this is to dropAllReferences before calling this.
  304. Function *removeFunctionFromModule(CallGraphNode *CGN) {
  305. return G->removeFunctionFromModule(CGN);
  306. }
  307. /// \brief Similar to operator[], but this will insert a new CallGraphNode for
  308. /// \c F if one does not already exist.
  309. CallGraphNode *getOrInsertFunction(const Function *F) {
  310. return G->getOrInsertFunction(F);
  311. }
  312. //===---------------------------------------------------------------------
  313. // Implementation of the ModulePass interface needed here.
  314. //
  315. void getAnalysisUsage(AnalysisUsage &AU) const override;
  316. bool runOnModule(Module &M) override;
  317. void releaseMemory() override;
  318. void print(raw_ostream &o, const Module *) const override;
  319. void dump() const;
  320. };
  321. // //
  322. ///////////////////////////////////////////////////////////////////////////////
  323. // GraphTraits specializations for call graphs so that they can be treated as
  324. // graphs by the generic graph algorithms.
  325. //
  326. // Provide graph traits for tranversing call graphs using standard graph
  327. // traversals.
  328. template <> struct GraphTraits<CallGraphNode *> {
  329. typedef CallGraphNode NodeType;
  330. typedef CallGraphNode::CallRecord CGNPairTy;
  331. typedef std::pointer_to_unary_function<CGNPairTy, CallGraphNode *>
  332. CGNDerefFun;
  333. static NodeType *getEntryNode(CallGraphNode *CGN) { return CGN; }
  334. typedef mapped_iterator<NodeType::iterator, CGNDerefFun> ChildIteratorType;
  335. static inline ChildIteratorType child_begin(NodeType *N) {
  336. return map_iterator(N->begin(), CGNDerefFun(CGNDeref));
  337. }
  338. static inline ChildIteratorType child_end(NodeType *N) {
  339. return map_iterator(N->end(), CGNDerefFun(CGNDeref));
  340. }
  341. static CallGraphNode *CGNDeref(CGNPairTy P) { return P.second; }
  342. };
  343. template <> struct GraphTraits<const CallGraphNode *> {
  344. typedef const CallGraphNode NodeType;
  345. typedef CallGraphNode::CallRecord CGNPairTy;
  346. typedef std::pointer_to_unary_function<CGNPairTy, const CallGraphNode *>
  347. CGNDerefFun;
  348. static NodeType *getEntryNode(const CallGraphNode *CGN) { return CGN; }
  349. typedef mapped_iterator<NodeType::const_iterator, CGNDerefFun>
  350. ChildIteratorType;
  351. static inline ChildIteratorType child_begin(NodeType *N) {
  352. return map_iterator(N->begin(), CGNDerefFun(CGNDeref));
  353. }
  354. static inline ChildIteratorType child_end(NodeType *N) {
  355. return map_iterator(N->end(), CGNDerefFun(CGNDeref));
  356. }
  357. static const CallGraphNode *CGNDeref(CGNPairTy P) { return P.second; }
  358. };
  359. template <>
  360. struct GraphTraits<CallGraph *> : public GraphTraits<CallGraphNode *> {
  361. static NodeType *getEntryNode(CallGraph *CGN) {
  362. return CGN->getExternalCallingNode(); // Start at the external node!
  363. }
  364. typedef std::pair<const Function *, CallGraphNode *> PairTy;
  365. typedef std::pointer_to_unary_function<PairTy, CallGraphNode &> DerefFun;
  366. // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
  367. typedef mapped_iterator<CallGraph::iterator, DerefFun> nodes_iterator;
  368. static nodes_iterator nodes_begin(CallGraph *CG) {
  369. return map_iterator(CG->begin(), DerefFun(CGdereference));
  370. }
  371. static nodes_iterator nodes_end(CallGraph *CG) {
  372. return map_iterator(CG->end(), DerefFun(CGdereference));
  373. }
  374. static CallGraphNode &CGdereference(PairTy P) { return *P.second; }
  375. };
  376. template <>
  377. struct GraphTraits<const CallGraph *> : public GraphTraits<
  378. const CallGraphNode *> {
  379. static NodeType *getEntryNode(const CallGraph *CGN) {
  380. return CGN->getExternalCallingNode(); // Start at the external node!
  381. }
  382. typedef std::pair<const Function *, const CallGraphNode *> PairTy;
  383. typedef std::pointer_to_unary_function<PairTy, const CallGraphNode &>
  384. DerefFun;
  385. // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
  386. typedef mapped_iterator<CallGraph::const_iterator, DerefFun> nodes_iterator;
  387. static nodes_iterator nodes_begin(const CallGraph *CG) {
  388. return map_iterator(CG->begin(), DerefFun(CGdereference));
  389. }
  390. static nodes_iterator nodes_end(const CallGraph *CG) {
  391. return map_iterator(CG->end(), DerefFun(CGdereference));
  392. }
  393. static const CallGraphNode &CGdereference(PairTy P) { return *P.second; }
  394. };
  395. } // End llvm namespace
  396. #endif