CallGraph.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- CallGraph.h - Build a Module's call graph ----------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. /// \file
  14. ///
  15. /// This file provides interfaces used to build and manipulate a call graph,
  16. /// which is a very useful tool for interprocedural optimization.
  17. ///
  18. /// Every function in a module is represented as a node in the call graph. The
  19. /// callgraph node keeps track of which functions are called by the function
  20. /// corresponding to the node.
  21. ///
  22. /// A call graph may contain nodes where the function that they correspond to
  23. /// is null. These 'external' nodes are used to represent control flow that is
  24. /// not represented (or analyzable) in the module. In particular, this
  25. /// analysis builds one external node such that:
  26. /// 1. All functions in the module without internal linkage will have edges
  27. /// from this external node, indicating that they could be called by
  28. /// functions outside of the module.
  29. /// 2. All functions whose address is used for something more than a direct
  30. /// call, for example being stored into a memory location will also have
  31. /// an edge from this external node. Since they may be called by an
  32. /// unknown caller later, they must be tracked as such.
  33. ///
  34. /// There is a second external node added for calls that leave this module.
  35. /// Functions have a call edge to the external node iff:
  36. /// 1. The function is external, reflecting the fact that they could call
  37. /// anything without internal linkage or that has its address taken.
  38. /// 2. The function contains an indirect function call.
  39. ///
  40. /// As an extension in the future, there may be multiple nodes with a null
  41. /// function. These will be used when we can prove (through pointer analysis)
  42. /// that an indirect call site can call only a specific set of functions.
  43. ///
  44. /// Because of these properties, the CallGraph captures a conservative superset
  45. /// of all of the caller-callee relationships, which is useful for
  46. /// transformations.
  47. ///
  48. //===----------------------------------------------------------------------===//
  49. #ifndef LLVM_ANALYSIS_CALLGRAPH_H
  50. #define LLVM_ANALYSIS_CALLGRAPH_H
  51. #include "llvm/IR/InstrTypes.h"
  52. #include "llvm/IR/Intrinsics.h"
  53. #include "llvm/IR/PassManager.h"
  54. #include "llvm/IR/ValueHandle.h"
  55. #include "llvm/Pass.h"
  56. #include <cassert>
  57. #include <map>
  58. #include <memory>
  59. #include <utility>
  60. #include <vector>
  61. namespace llvm {
  62. template <class GraphType> struct GraphTraits;
  63. class CallGraphNode;
  64. class Function;
  65. class Module;
  66. class raw_ostream;
  67. /// The basic data container for the call graph of a \c Module of IR.
  68. ///
  69. /// This class exposes both the interface to the call graph for a module of IR.
  70. ///
  71. /// The core call graph itself can also be updated to reflect changes to the IR.
  72. class CallGraph {
  73. Module &M;
  74. using FunctionMapTy =
  75. std::map<const Function *, std::unique_ptr<CallGraphNode>>;
  76. /// A map from \c Function* to \c CallGraphNode*.
  77. FunctionMapTy FunctionMap;
  78. /// This node has edges to all external functions and those internal
  79. /// functions that have their address taken.
  80. CallGraphNode *ExternalCallingNode;
  81. /// 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. public:
  85. explicit CallGraph(Module &M);
  86. CallGraph(CallGraph &&Arg);
  87. ~CallGraph();
  88. void print(raw_ostream &OS) const;
  89. void dump() const;
  90. using iterator = FunctionMapTy::iterator;
  91. using const_iterator = FunctionMapTy::const_iterator;
  92. /// Returns the module the call graph corresponds to.
  93. Module &getModule() const { return M; }
  94. bool invalidate(Module &, const PreservedAnalyses &PA,
  95. ModuleAnalysisManager::Invalidator &);
  96. inline iterator begin() { return FunctionMap.begin(); }
  97. inline iterator end() { return FunctionMap.end(); }
  98. inline const_iterator begin() const { return FunctionMap.begin(); }
  99. inline const_iterator end() const { return FunctionMap.end(); }
  100. /// Returns the call graph node for the provided function.
  101. inline const CallGraphNode *operator[](const Function *F) const {
  102. const_iterator I = FunctionMap.find(F);
  103. assert(I != FunctionMap.end() && "Function not in callgraph!");
  104. return I->second.get();
  105. }
  106. /// Returns the call graph node for the provided function.
  107. inline CallGraphNode *operator[](const Function *F) {
  108. const_iterator I = FunctionMap.find(F);
  109. assert(I != FunctionMap.end() && "Function not in callgraph!");
  110. return I->second.get();
  111. }
  112. /// Returns the \c CallGraphNode which is used to represent
  113. /// undetermined calls into the callgraph.
  114. CallGraphNode *getExternalCallingNode() const { return ExternalCallingNode; }
  115. CallGraphNode *getCallsExternalNode() const {
  116. return CallsExternalNode.get();
  117. }
  118. /// Old node has been deleted, and New is to be used in its place, update the
  119. /// ExternalCallingNode.
  120. void ReplaceExternalCallEdge(CallGraphNode *Old, CallGraphNode *New);
  121. //===---------------------------------------------------------------------
  122. // Functions to keep a call graph up to date with a function that has been
  123. // modified.
  124. //
  125. /// Unlink the function from this module, returning it.
  126. ///
  127. /// Because this removes the function from the module, the call graph node is
  128. /// destroyed. This is only valid if the function does not call any other
  129. /// functions (ie, there are no edges in it's CGN). The easiest way to do
  130. /// this is to dropAllReferences before calling this.
  131. Function *removeFunctionFromModule(CallGraphNode *CGN);
  132. /// Similar to operator[], but this will insert a new CallGraphNode for
  133. /// \c F if one does not already exist.
  134. CallGraphNode *getOrInsertFunction(const Function *F);
  135. /// Populate \p CGN based on the calls inside the associated function.
  136. void populateCallGraphNode(CallGraphNode *CGN);
  137. /// Add a function to the call graph, and link the node to all of the
  138. /// functions that it calls.
  139. void addToCallGraph(Function *F);
  140. };
  141. /// A node in the call graph for a module.
  142. ///
  143. /// Typically represents a function in the call graph. There are also special
  144. /// "null" nodes used to represent theoretical entries in the call graph.
  145. class CallGraphNode {
  146. public:
  147. /// A pair of the calling instruction (a call or invoke)
  148. /// and the call graph node being called.
  149. /// Call graph node may have two types of call records which represent an edge
  150. /// in the call graph - reference or a call edge. Reference edges are not
  151. /// associated with any call instruction and are created with the first field
  152. /// set to `None`, while real call edges have instruction address in this
  153. /// field. Therefore, all real call edges are expected to have a value in the
  154. /// first field and it is not supposed to be `nullptr`.
  155. /// Reference edges, for example, are used for connecting broker function
  156. /// caller to the callback function for callback call sites.
  157. using CallRecord = std::pair<std::optional<WeakTrackingVH>, CallGraphNode *>;
  158. public:
  159. using CalledFunctionsVector = std::vector<CallRecord>;
  160. /// Creates a node for the specified function.
  161. inline CallGraphNode(CallGraph *CG, Function *F) : CG(CG), F(F) {}
  162. CallGraphNode(const CallGraphNode &) = delete;
  163. CallGraphNode &operator=(const CallGraphNode &) = delete;
  164. ~CallGraphNode() {
  165. assert(NumReferences == 0 && "Node deleted while references remain");
  166. }
  167. using iterator = std::vector<CallRecord>::iterator;
  168. using const_iterator = std::vector<CallRecord>::const_iterator;
  169. /// Returns the function that this call graph node represents.
  170. Function *getFunction() const { return F; }
  171. inline iterator begin() { return CalledFunctions.begin(); }
  172. inline iterator end() { return CalledFunctions.end(); }
  173. inline const_iterator begin() const { return CalledFunctions.begin(); }
  174. inline const_iterator end() const { return CalledFunctions.end(); }
  175. inline bool empty() const { return CalledFunctions.empty(); }
  176. inline unsigned size() const { return (unsigned)CalledFunctions.size(); }
  177. /// Returns the number of other CallGraphNodes in this CallGraph that
  178. /// reference this node in their callee list.
  179. unsigned getNumReferences() const { return NumReferences; }
  180. /// Returns the i'th called function.
  181. CallGraphNode *operator[](unsigned i) const {
  182. assert(i < CalledFunctions.size() && "Invalid index");
  183. return CalledFunctions[i].second;
  184. }
  185. /// Print out this call graph node.
  186. void dump() const;
  187. void print(raw_ostream &OS) const;
  188. //===---------------------------------------------------------------------
  189. // Methods to keep a call graph up to date with a function that has been
  190. // modified
  191. //
  192. /// Removes all edges from this CallGraphNode to any functions it
  193. /// calls.
  194. void removeAllCalledFunctions() {
  195. while (!CalledFunctions.empty()) {
  196. CalledFunctions.back().second->DropRef();
  197. CalledFunctions.pop_back();
  198. }
  199. }
  200. /// Moves all the callee information from N to this node.
  201. void stealCalledFunctionsFrom(CallGraphNode *N) {
  202. assert(CalledFunctions.empty() &&
  203. "Cannot steal callsite information if I already have some");
  204. std::swap(CalledFunctions, N->CalledFunctions);
  205. }
  206. /// Adds a function to the list of functions called by this one.
  207. void addCalledFunction(CallBase *Call, CallGraphNode *M) {
  208. CalledFunctions.emplace_back(Call ? std::optional<WeakTrackingVH>(Call)
  209. : std::optional<WeakTrackingVH>(),
  210. M);
  211. M->AddRef();
  212. }
  213. void removeCallEdge(iterator I) {
  214. I->second->DropRef();
  215. *I = CalledFunctions.back();
  216. CalledFunctions.pop_back();
  217. }
  218. /// Removes the edge in the node for the specified call site.
  219. ///
  220. /// Note that this method takes linear time, so it should be used sparingly.
  221. void removeCallEdgeFor(CallBase &Call);
  222. /// Removes all call edges from this node to the specified callee
  223. /// function.
  224. ///
  225. /// This takes more time to execute than removeCallEdgeTo, so it should not
  226. /// be used unless necessary.
  227. void removeAnyCallEdgeTo(CallGraphNode *Callee);
  228. /// Removes one edge associated with a null callsite from this node to
  229. /// the specified callee function.
  230. void removeOneAbstractEdgeTo(CallGraphNode *Callee);
  231. /// Replaces the edge in the node for the specified call site with a
  232. /// new one.
  233. ///
  234. /// Note that this method takes linear time, so it should be used sparingly.
  235. void replaceCallEdge(CallBase &Call, CallBase &NewCall,
  236. CallGraphNode *NewNode);
  237. private:
  238. friend class CallGraph;
  239. CallGraph *CG;
  240. Function *F;
  241. std::vector<CallRecord> CalledFunctions;
  242. /// The number of times that this CallGraphNode occurs in the
  243. /// CalledFunctions array of this or other CallGraphNodes.
  244. unsigned NumReferences = 0;
  245. void DropRef() { --NumReferences; }
  246. void AddRef() { ++NumReferences; }
  247. /// A special function that should only be used by the CallGraph class.
  248. void allReferencesDropped() { NumReferences = 0; }
  249. };
  250. /// An analysis pass to compute the \c CallGraph for a \c Module.
  251. ///
  252. /// This class implements the concept of an analysis pass used by the \c
  253. /// ModuleAnalysisManager to run an analysis over a module and cache the
  254. /// resulting data.
  255. class CallGraphAnalysis : public AnalysisInfoMixin<CallGraphAnalysis> {
  256. friend AnalysisInfoMixin<CallGraphAnalysis>;
  257. static AnalysisKey Key;
  258. public:
  259. /// A formulaic type to inform clients of the result type.
  260. using Result = CallGraph;
  261. /// Compute the \c CallGraph for the module \c M.
  262. ///
  263. /// The real work here is done in the \c CallGraph constructor.
  264. CallGraph run(Module &M, ModuleAnalysisManager &) { return CallGraph(M); }
  265. };
  266. /// Printer pass for the \c CallGraphAnalysis results.
  267. class CallGraphPrinterPass : public PassInfoMixin<CallGraphPrinterPass> {
  268. raw_ostream &OS;
  269. public:
  270. explicit CallGraphPrinterPass(raw_ostream &OS) : OS(OS) {}
  271. PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
  272. };
  273. /// Printer pass for the summarized \c CallGraphAnalysis results.
  274. class CallGraphSCCsPrinterPass
  275. : public PassInfoMixin<CallGraphSCCsPrinterPass> {
  276. raw_ostream &OS;
  277. public:
  278. explicit CallGraphSCCsPrinterPass(raw_ostream &OS) : OS(OS) {}
  279. PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
  280. };
  281. /// The \c ModulePass which wraps up a \c CallGraph and the logic to
  282. /// build it.
  283. ///
  284. /// This class exposes both the interface to the call graph container and the
  285. /// module pass which runs over a module of IR and produces the call graph. The
  286. /// call graph interface is entirelly a wrapper around a \c CallGraph object
  287. /// which is stored internally for each module.
  288. class CallGraphWrapperPass : public ModulePass {
  289. std::unique_ptr<CallGraph> G;
  290. public:
  291. static char ID; // Class identification, replacement for typeinfo
  292. CallGraphWrapperPass();
  293. ~CallGraphWrapperPass() override;
  294. /// The internal \c CallGraph around which the rest of this interface
  295. /// is wrapped.
  296. const CallGraph &getCallGraph() const { return *G; }
  297. CallGraph &getCallGraph() { return *G; }
  298. using iterator = CallGraph::iterator;
  299. using const_iterator = CallGraph::const_iterator;
  300. /// Returns the module the call graph corresponds to.
  301. Module &getModule() const { return G->getModule(); }
  302. inline iterator begin() { return G->begin(); }
  303. inline iterator end() { return G->end(); }
  304. inline const_iterator begin() const { return G->begin(); }
  305. inline const_iterator end() const { return G->end(); }
  306. /// Returns the call graph node for the provided function.
  307. inline const CallGraphNode *operator[](const Function *F) const {
  308. return (*G)[F];
  309. }
  310. /// Returns the call graph node for the provided function.
  311. inline CallGraphNode *operator[](const Function *F) { return (*G)[F]; }
  312. /// Returns the \c CallGraphNode which is used to represent
  313. /// undetermined calls into the callgraph.
  314. CallGraphNode *getExternalCallingNode() const {
  315. return G->getExternalCallingNode();
  316. }
  317. CallGraphNode *getCallsExternalNode() const {
  318. return G->getCallsExternalNode();
  319. }
  320. //===---------------------------------------------------------------------
  321. // Functions to keep a call graph up to date with a function that has been
  322. // modified.
  323. //
  324. /// Unlink the function from this module, returning it.
  325. ///
  326. /// Because this removes the function from the module, the call graph node is
  327. /// destroyed. This is only valid if the function does not call any other
  328. /// functions (ie, there are no edges in it's CGN). The easiest way to do
  329. /// this is to dropAllReferences before calling this.
  330. Function *removeFunctionFromModule(CallGraphNode *CGN) {
  331. return G->removeFunctionFromModule(CGN);
  332. }
  333. /// Similar to operator[], but this will insert a new CallGraphNode for
  334. /// \c F if one does not already exist.
  335. CallGraphNode *getOrInsertFunction(const Function *F) {
  336. return G->getOrInsertFunction(F);
  337. }
  338. //===---------------------------------------------------------------------
  339. // Implementation of the ModulePass interface needed here.
  340. //
  341. void getAnalysisUsage(AnalysisUsage &AU) const override;
  342. bool runOnModule(Module &M) override;
  343. void releaseMemory() override;
  344. void print(raw_ostream &o, const Module *) const override;
  345. void dump() const;
  346. };
  347. //===----------------------------------------------------------------------===//
  348. // GraphTraits specializations for call graphs so that they can be treated as
  349. // graphs by the generic graph algorithms.
  350. //
  351. // Provide graph traits for traversing call graphs using standard graph
  352. // traversals.
  353. template <> struct GraphTraits<CallGraphNode *> {
  354. using NodeRef = CallGraphNode *;
  355. using CGNPairTy = CallGraphNode::CallRecord;
  356. static NodeRef getEntryNode(CallGraphNode *CGN) { return CGN; }
  357. static CallGraphNode *CGNGetValue(CGNPairTy P) { return P.second; }
  358. using ChildIteratorType =
  359. mapped_iterator<CallGraphNode::iterator, decltype(&CGNGetValue)>;
  360. static ChildIteratorType child_begin(NodeRef N) {
  361. return ChildIteratorType(N->begin(), &CGNGetValue);
  362. }
  363. static ChildIteratorType child_end(NodeRef N) {
  364. return ChildIteratorType(N->end(), &CGNGetValue);
  365. }
  366. };
  367. template <> struct GraphTraits<const CallGraphNode *> {
  368. using NodeRef = const CallGraphNode *;
  369. using CGNPairTy = CallGraphNode::CallRecord;
  370. using EdgeRef = const CallGraphNode::CallRecord &;
  371. static NodeRef getEntryNode(const CallGraphNode *CGN) { return CGN; }
  372. static const CallGraphNode *CGNGetValue(CGNPairTy P) { return P.second; }
  373. using ChildIteratorType =
  374. mapped_iterator<CallGraphNode::const_iterator, decltype(&CGNGetValue)>;
  375. using ChildEdgeIteratorType = CallGraphNode::const_iterator;
  376. static ChildIteratorType child_begin(NodeRef N) {
  377. return ChildIteratorType(N->begin(), &CGNGetValue);
  378. }
  379. static ChildIteratorType child_end(NodeRef N) {
  380. return ChildIteratorType(N->end(), &CGNGetValue);
  381. }
  382. static ChildEdgeIteratorType child_edge_begin(NodeRef N) {
  383. return N->begin();
  384. }
  385. static ChildEdgeIteratorType child_edge_end(NodeRef N) { return N->end(); }
  386. static NodeRef edge_dest(EdgeRef E) { return E.second; }
  387. };
  388. template <>
  389. struct GraphTraits<CallGraph *> : public GraphTraits<CallGraphNode *> {
  390. using PairTy =
  391. std::pair<const Function *const, std::unique_ptr<CallGraphNode>>;
  392. static NodeRef getEntryNode(CallGraph *CGN) {
  393. return CGN->getExternalCallingNode(); // Start at the external node!
  394. }
  395. static CallGraphNode *CGGetValuePtr(const PairTy &P) {
  396. return P.second.get();
  397. }
  398. // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
  399. using nodes_iterator =
  400. mapped_iterator<CallGraph::iterator, decltype(&CGGetValuePtr)>;
  401. static nodes_iterator nodes_begin(CallGraph *CG) {
  402. return nodes_iterator(CG->begin(), &CGGetValuePtr);
  403. }
  404. static nodes_iterator nodes_end(CallGraph *CG) {
  405. return nodes_iterator(CG->end(), &CGGetValuePtr);
  406. }
  407. };
  408. template <>
  409. struct GraphTraits<const CallGraph *> : public GraphTraits<
  410. const CallGraphNode *> {
  411. using PairTy =
  412. std::pair<const Function *const, std::unique_ptr<CallGraphNode>>;
  413. static NodeRef getEntryNode(const CallGraph *CGN) {
  414. return CGN->getExternalCallingNode(); // Start at the external node!
  415. }
  416. static const CallGraphNode *CGGetValuePtr(const PairTy &P) {
  417. return P.second.get();
  418. }
  419. // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
  420. using nodes_iterator =
  421. mapped_iterator<CallGraph::const_iterator, decltype(&CGGetValuePtr)>;
  422. static nodes_iterator nodes_begin(const CallGraph *CG) {
  423. return nodes_iterator(CG->begin(), &CGGetValuePtr);
  424. }
  425. static nodes_iterator nodes_end(const CallGraph *CG) {
  426. return nodes_iterator(CG->end(), &CGGetValuePtr);
  427. }
  428. };
  429. } // end namespace llvm
  430. #endif // LLVM_ANALYSIS_CALLGRAPH_H
  431. #ifdef __GNUC__
  432. #pragma GCC diagnostic pop
  433. #endif