CallGraph.h 19 KB

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