CallGraph.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 *, std::unique_ptr<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. std::unique_ptr<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. explicit CallGraph(Module &M);
  96. CallGraph(CallGraph &&Arg);
  97. ~CallGraph();
  98. void print(raw_ostream &OS) const;
  99. void dump() const;
  100. typedef FunctionMapTy::iterator iterator;
  101. typedef FunctionMapTy::const_iterator const_iterator;
  102. /// \brief Returns the module the call graph corresponds to.
  103. Module &getModule() const { return M; }
  104. inline iterator begin() { return FunctionMap.begin(); }
  105. inline iterator end() { return FunctionMap.end(); }
  106. inline const_iterator begin() const { return FunctionMap.begin(); }
  107. inline const_iterator end() const { return FunctionMap.end(); }
  108. /// \brief Returns the call graph node for the provided function.
  109. inline const CallGraphNode *operator[](const Function *F) const {
  110. const_iterator I = FunctionMap.find(F);
  111. assert(I != FunctionMap.end() && "Function not in callgraph!");
  112. return I->second.get();
  113. }
  114. /// \brief Returns the call graph node for the provided function.
  115. inline CallGraphNode *operator[](const Function *F) {
  116. const_iterator I = FunctionMap.find(F);
  117. assert(I != FunctionMap.end() && "Function not in callgraph!");
  118. return I->second.get();
  119. }
  120. /// \brief Returns the \c CallGraphNode which is used to represent
  121. /// undetermined calls into the callgraph.
  122. CallGraphNode *getExternalCallingNode() const { return ExternalCallingNode; }
  123. CallGraphNode *getCallsExternalNode() const { return CallsExternalNode.get(); }
  124. //===---------------------------------------------------------------------
  125. // Functions to keep a call graph up to date with a function that has been
  126. // modified.
  127. //
  128. /// \brief Unlink the function from this module, returning it.
  129. ///
  130. /// Because this removes the function from the module, the call graph node is
  131. /// destroyed. This is only valid if the function does not call any other
  132. /// functions (ie, there are no edges in it's CGN). The easiest way to do
  133. /// this is to dropAllReferences before calling this.
  134. Function *removeFunctionFromModule(CallGraphNode *CGN);
  135. /// \brief Similar to operator[], but this will insert a new CallGraphNode for
  136. /// \c F if one does not already exist.
  137. CallGraphNode *getOrInsertFunction(const Function *F);
  138. };
  139. /// \brief A node in the call graph for a module.
  140. ///
  141. /// Typically represents a function in the call graph. There are also special
  142. /// "null" nodes used to represent theoretical entries in the call graph.
  143. class CallGraphNode {
  144. public:
  145. /// \brief A pair of the calling instruction (a call or invoke)
  146. /// and the call graph node being called.
  147. typedef std::pair<WeakVH, CallGraphNode *> CallRecord;
  148. public:
  149. typedef std::vector<CallRecord> CalledFunctionsVector;
  150. /// \brief Creates a node for the specified function.
  151. inline CallGraphNode(Function *F) : F(F), NumReferences(0) {}
  152. ~CallGraphNode() {
  153. assert(NumReferences == 0 && "Node deleted while references remain");
  154. }
  155. typedef std::vector<CallRecord>::iterator iterator;
  156. typedef std::vector<CallRecord>::const_iterator const_iterator;
  157. /// \brief Returns the function that this call graph node represents.
  158. Function *getFunction() const { return F; }
  159. inline iterator begin() { return CalledFunctions.begin(); }
  160. inline iterator end() { return CalledFunctions.end(); }
  161. inline const_iterator begin() const { return CalledFunctions.begin(); }
  162. inline const_iterator end() const { return CalledFunctions.end(); }
  163. inline bool empty() const { return CalledFunctions.empty(); }
  164. inline unsigned size() const { return (unsigned)CalledFunctions.size(); }
  165. /// \brief Returns the number of other CallGraphNodes in this CallGraph that
  166. /// reference this node in their callee list.
  167. unsigned getNumReferences() const { return NumReferences; }
  168. /// \brief Returns the i'th called function.
  169. CallGraphNode *operator[](unsigned i) const {
  170. assert(i < CalledFunctions.size() && "Invalid index");
  171. return CalledFunctions[i].second;
  172. }
  173. /// \brief Print out this call graph node.
  174. void dump() const;
  175. void print(raw_ostream &OS) const;
  176. //===---------------------------------------------------------------------
  177. // Methods to keep a call graph up to date with a function that has been
  178. // modified
  179. //
  180. /// \brief Removes all edges from this CallGraphNode to any functions it
  181. /// calls.
  182. void removeAllCalledFunctions() {
  183. while (!CalledFunctions.empty()) {
  184. CalledFunctions.back().second->DropRef();
  185. CalledFunctions.pop_back();
  186. }
  187. }
  188. /// \brief Moves all the callee information from N to this node.
  189. void stealCalledFunctionsFrom(CallGraphNode *N) {
  190. assert(CalledFunctions.empty() &&
  191. "Cannot steal callsite information if I already have some");
  192. std::swap(CalledFunctions, N->CalledFunctions);
  193. }
  194. /// \brief Adds a function to the list of functions called by this one.
  195. void addCalledFunction(CallSite CS, CallGraphNode *M) {
  196. assert(!CS.getInstruction() || !CS.getCalledFunction() ||
  197. !CS.getCalledFunction()->isIntrinsic() ||
  198. !Intrinsic::isLeaf(CS.getCalledFunction()->getIntrinsicID()));
  199. CalledFunctions.emplace_back(CS.getInstruction(), M);
  200. M->AddRef();
  201. }
  202. void removeCallEdge(iterator I) {
  203. I->second->DropRef();
  204. *I = CalledFunctions.back();
  205. CalledFunctions.pop_back();
  206. }
  207. /// \brief Removes the edge in the node for the specified call site.
  208. ///
  209. /// Note that this method takes linear time, so it should be used sparingly.
  210. void removeCallEdgeFor(CallSite CS);
  211. /// \brief Removes all call edges from this node to the specified callee
  212. /// function.
  213. ///
  214. /// This takes more time to execute than removeCallEdgeTo, so it should not
  215. /// be used unless necessary.
  216. void removeAnyCallEdgeTo(CallGraphNode *Callee);
  217. /// \brief Removes one edge associated with a null callsite from this node to
  218. /// the specified callee function.
  219. void removeOneAbstractEdgeTo(CallGraphNode *Callee);
  220. /// \brief Replaces the edge in the node for the specified call site with a
  221. /// new one.
  222. ///
  223. /// Note that this method takes linear time, so it should be used sparingly.
  224. void replaceCallEdge(CallSite CS, CallSite NewCS, CallGraphNode *NewNode);
  225. private:
  226. friend class CallGraph;
  227. AssertingVH<Function> F;
  228. std::vector<CallRecord> CalledFunctions;
  229. /// \brief The number of times that this CallGraphNode occurs in the
  230. /// CalledFunctions array of this or other CallGraphNodes.
  231. unsigned NumReferences;
  232. CallGraphNode(const CallGraphNode &) = delete;
  233. void operator=(const CallGraphNode &) = delete;
  234. void DropRef() { --NumReferences; }
  235. void AddRef() { ++NumReferences; }
  236. /// \brief A special function that should only be used by the CallGraph class.
  237. void allReferencesDropped() { NumReferences = 0; }
  238. };
  239. /// \brief An analysis pass to compute the \c CallGraph for a \c Module.
  240. ///
  241. /// This class implements the concept of an analysis pass used by the \c
  242. /// ModuleAnalysisManager to run an analysis over a module and cache the
  243. /// resulting data.
  244. class CallGraphAnalysis {
  245. public:
  246. /// \brief A formulaic typedef to inform clients of the result type.
  247. typedef CallGraph Result;
  248. static void *ID() { return (void *)&PassID; }
  249. /// \brief Compute the \c CallGraph for the module \c M.
  250. ///
  251. /// The real work here is done in the \c CallGraph constructor.
  252. CallGraph run(Module *M) { return CallGraph(*M); }
  253. private:
  254. static char PassID;
  255. };
  256. /// \brief The \c ModulePass which wraps up a \c CallGraph and the logic to
  257. /// build it.
  258. ///
  259. /// This class exposes both the interface to the call graph container and the
  260. /// module pass which runs over a module of IR and produces the call graph. The
  261. /// call graph interface is entirelly a wrapper around a \c CallGraph object
  262. /// which is stored internally for each module.
  263. class CallGraphWrapperPass : public ModulePass {
  264. std::unique_ptr<CallGraph> G;
  265. public:
  266. static char ID; // Class identification, replacement for typeinfo
  267. CallGraphWrapperPass();
  268. ~CallGraphWrapperPass() override;
  269. /// \brief The internal \c CallGraph around which the rest of this interface
  270. /// is wrapped.
  271. const CallGraph &getCallGraph() const { return *G; }
  272. CallGraph &getCallGraph() { return *G; }
  273. typedef CallGraph::iterator iterator;
  274. typedef CallGraph::const_iterator const_iterator;
  275. /// \brief Returns the module the call graph corresponds to.
  276. Module &getModule() const { return G->getModule(); }
  277. inline iterator begin() { return G->begin(); }
  278. inline iterator end() { return G->end(); }
  279. inline const_iterator begin() const { return G->begin(); }
  280. inline const_iterator end() const { return G->end(); }
  281. /// \brief Returns the call graph node for the provided function.
  282. inline const CallGraphNode *operator[](const Function *F) const {
  283. return (*G)[F];
  284. }
  285. /// \brief Returns the call graph node for the provided function.
  286. inline CallGraphNode *operator[](const Function *F) { return (*G)[F]; }
  287. /// \brief Returns the \c CallGraphNode which is used to represent
  288. /// undetermined calls into the callgraph.
  289. CallGraphNode *getExternalCallingNode() const {
  290. return G->getExternalCallingNode();
  291. }
  292. CallGraphNode *getCallsExternalNode() const {
  293. return G->getCallsExternalNode();
  294. }
  295. //===---------------------------------------------------------------------
  296. // Functions to keep a call graph up to date with a function that has been
  297. // modified.
  298. //
  299. /// \brief Unlink the function from this module, returning it.
  300. ///
  301. /// Because this removes the function from the module, the call graph node is
  302. /// destroyed. This is only valid if the function does not call any other
  303. /// functions (ie, there are no edges in it's CGN). The easiest way to do
  304. /// this is to dropAllReferences before calling this.
  305. Function *removeFunctionFromModule(CallGraphNode *CGN) {
  306. return G->removeFunctionFromModule(CGN);
  307. }
  308. /// \brief Similar to operator[], but this will insert a new CallGraphNode for
  309. /// \c F if one does not already exist.
  310. CallGraphNode *getOrInsertFunction(const Function *F) {
  311. return G->getOrInsertFunction(F);
  312. }
  313. //===---------------------------------------------------------------------
  314. // Implementation of the ModulePass interface needed here.
  315. //
  316. void getAnalysisUsage(AnalysisUsage &AU) const override;
  317. bool runOnModule(Module &M) override;
  318. void releaseMemory() override;
  319. void print(raw_ostream &o, const Module *) const override;
  320. void dump() const;
  321. };
  322. // //
  323. ///////////////////////////////////////////////////////////////////////////////
  324. // GraphTraits specializations for call graphs so that they can be treated as
  325. // graphs by the generic graph algorithms.
  326. //
  327. // Provide graph traits for tranversing call graphs using standard graph
  328. // traversals.
  329. template <> struct GraphTraits<CallGraphNode *> {
  330. typedef CallGraphNode NodeType;
  331. typedef CallGraphNode::CallRecord CGNPairTy;
  332. static NodeType *getEntryNode(CallGraphNode *CGN) { return CGN; }
  333. static CallGraphNode *CGNGetValue(CGNPairTy P) { return P.second; }
  334. typedef mapped_iterator<NodeType::iterator, decltype(&CGNGetValue)> ChildIteratorType;
  335. static inline ChildIteratorType child_begin(NodeType *N) {
  336. return ChildIteratorType(N->begin(), &CGNGetValue);
  337. }
  338. static inline ChildIteratorType child_end(NodeType *N) {
  339. return ChildIteratorType(N->end(), &CGNGetValue);
  340. }
  341. };
  342. template <> struct GraphTraits<const CallGraphNode *> {
  343. typedef const CallGraphNode NodeType;
  344. typedef CallGraphNode::CallRecord CGNPairTy;
  345. static NodeType *getEntryNode(const CallGraphNode *CGN) { return CGN; }
  346. static const CallGraphNode *CGNGetValue(CGNPairTy P) { return P.second; }
  347. typedef mapped_iterator<NodeType::const_iterator, decltype(&CGNGetValue)>
  348. ChildIteratorType;
  349. static inline ChildIteratorType child_begin(NodeType *N) {
  350. return ChildIteratorType(N->begin(), &CGNGetValue);
  351. }
  352. static inline ChildIteratorType child_end(NodeType *N) {
  353. return ChildIteratorType(N->end(), &CGNGetValue);
  354. }
  355. };
  356. template <>
  357. struct GraphTraits<CallGraph *> : public GraphTraits<CallGraphNode *> {
  358. static NodeType *getEntryNode(CallGraph *CGN) {
  359. return CGN->getExternalCallingNode(); // Start at the external node!
  360. }
  361. typedef std::pair<const Function *const, std::unique_ptr<CallGraphNode>>
  362. PairTy;
  363. static CallGraphNode *CGGetValuePtr(const PairTy &P) {
  364. return P.second.get();
  365. }
  366. // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
  367. typedef mapped_iterator<CallGraph::iterator, decltype(&CGGetValuePtr)> nodes_iterator;
  368. static nodes_iterator nodes_begin(CallGraph *CG) {
  369. return nodes_iterator(CG->begin(), &CGGetValuePtr);
  370. }
  371. static nodes_iterator nodes_end(CallGraph *CG) {
  372. return nodes_iterator(CG->end(), &CGGetValuePtr);
  373. }
  374. };
  375. template <>
  376. struct GraphTraits<const CallGraph *> : public GraphTraits<
  377. const CallGraphNode *> {
  378. static NodeType *getEntryNode(const CallGraph *CGN) {
  379. return CGN->getExternalCallingNode(); // Start at the external node!
  380. }
  381. typedef std::pair<const Function *const, std::unique_ptr<CallGraphNode>>
  382. PairTy;
  383. static const CallGraphNode *CGGetValuePtr(const PairTy &P) {
  384. return P.second.get();
  385. }
  386. // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
  387. typedef mapped_iterator<CallGraph::const_iterator, decltype(&CGGetValuePtr)>
  388. nodes_iterator;
  389. static nodes_iterator nodes_begin(const CallGraph *CG) {
  390. return nodes_iterator(CG->begin(), &CGGetValuePtr);
  391. }
  392. static nodes_iterator nodes_end(const CallGraph *CG) {
  393. return nodes_iterator(CG->end(), &CGGetValuePtr);
  394. }
  395. };
  396. } // End llvm namespace
  397. #endif