CGSCCPassManager.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. //===- CGSCCPassManager.h - Call graph pass management ----------*- 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 header provides classes for managing passes over SCCs of the call
  12. /// graph. These passes form an important component of LLVM's interprocedural
  13. /// optimizations. Because they operate on the SCCs of the call graph, and they
  14. /// wtraverse the graph in post order, they can effectively do pair-wise
  15. /// interprocedural optimizations for all call edges in the program. At each
  16. /// call site edge, the callee has already been optimized as much as is
  17. /// possible. This in turn allows very accurate analysis of it for IPO.
  18. ///
  19. //===----------------------------------------------------------------------===//
  20. #ifndef LLVM_ANALYSIS_CGSCCPASSMANAGER_H
  21. #define LLVM_ANALYSIS_CGSCCPASSMANAGER_H
  22. #include "llvm/Analysis/LazyCallGraph.h"
  23. #include "llvm/IR/PassManager.h"
  24. namespace llvm {
  25. /// \brief The CGSCC pass manager.
  26. ///
  27. /// See the documentation for the PassManager template for details. It runs
  28. /// a sequency of SCC passes over each SCC that the manager is run over. This
  29. /// typedef serves as a convenient way to refer to this construct.
  30. typedef PassManager<LazyCallGraph::SCC> CGSCCPassManager;
  31. /// \brief The CGSCC analysis manager.
  32. ///
  33. /// See the documentation for the AnalysisManager template for detail
  34. /// documentation. This typedef serves as a convenient way to refer to this
  35. /// construct in the adaptors and proxies used to integrate this into the larger
  36. /// pass manager infrastructure.
  37. typedef AnalysisManager<LazyCallGraph::SCC> CGSCCAnalysisManager;
  38. /// \brief A module analysis which acts as a proxy for a CGSCC analysis
  39. /// manager.
  40. ///
  41. /// This primarily proxies invalidation information from the module analysis
  42. /// manager and module pass manager to a CGSCC analysis manager. You should
  43. /// never use a CGSCC analysis manager from within (transitively) a module
  44. /// pass manager unless your parent module pass has received a proxy result
  45. /// object for it.
  46. class CGSCCAnalysisManagerModuleProxy {
  47. public:
  48. class Result {
  49. public:
  50. explicit Result(CGSCCAnalysisManager &CGAM) : CGAM(&CGAM) {}
  51. // We have to explicitly define all the special member functions because
  52. // MSVC refuses to generate them.
  53. Result(const Result &Arg) : CGAM(Arg.CGAM) {}
  54. Result(Result &&Arg) : CGAM(std::move(Arg.CGAM)) {}
  55. Result &operator=(Result RHS) {
  56. std::swap(CGAM, RHS.CGAM);
  57. return *this;
  58. }
  59. ~Result();
  60. /// \brief Accessor for the \c CGSCCAnalysisManager.
  61. CGSCCAnalysisManager &getManager() { return *CGAM; }
  62. /// \brief Handler for invalidation of the module.
  63. ///
  64. /// If this analysis itself is preserved, then we assume that the call
  65. /// graph of the module hasn't changed and thus we don't need to invalidate
  66. /// *all* cached data associated with a \c SCC* in the \c
  67. /// CGSCCAnalysisManager.
  68. ///
  69. /// Regardless of whether this analysis is marked as preserved, all of the
  70. /// analyses in the \c CGSCCAnalysisManager are potentially invalidated
  71. /// based on the set of preserved analyses.
  72. bool invalidate(Module &M, const PreservedAnalyses &PA);
  73. private:
  74. CGSCCAnalysisManager *CGAM;
  75. };
  76. static void *ID() { return (void *)&PassID; }
  77. static StringRef name() { return "CGSCCAnalysisManagerModuleProxy"; }
  78. explicit CGSCCAnalysisManagerModuleProxy(CGSCCAnalysisManager &CGAM)
  79. : CGAM(&CGAM) {}
  80. // We have to explicitly define all the special member functions because MSVC
  81. // refuses to generate them.
  82. CGSCCAnalysisManagerModuleProxy(const CGSCCAnalysisManagerModuleProxy &Arg)
  83. : CGAM(Arg.CGAM) {}
  84. CGSCCAnalysisManagerModuleProxy(CGSCCAnalysisManagerModuleProxy &&Arg)
  85. : CGAM(std::move(Arg.CGAM)) {}
  86. CGSCCAnalysisManagerModuleProxy &
  87. operator=(CGSCCAnalysisManagerModuleProxy RHS) {
  88. std::swap(CGAM, RHS.CGAM);
  89. return *this;
  90. }
  91. /// \brief Run the analysis pass and create our proxy result object.
  92. ///
  93. /// This doesn't do any interesting work, it is primarily used to insert our
  94. /// proxy result object into the module analysis cache so that we can proxy
  95. /// invalidation to the CGSCC analysis manager.
  96. ///
  97. /// In debug builds, it will also assert that the analysis manager is empty
  98. /// as no queries should arrive at the CGSCC analysis manager prior to
  99. /// this analysis being requested.
  100. Result run(Module &M);
  101. private:
  102. static char PassID;
  103. CGSCCAnalysisManager *CGAM;
  104. };
  105. /// \brief A CGSCC analysis which acts as a proxy for a module analysis
  106. /// manager.
  107. ///
  108. /// This primarily provides an accessor to a parent module analysis manager to
  109. /// CGSCC passes. Only the const interface of the module analysis manager is
  110. /// provided to indicate that once inside of a CGSCC analysis pass you
  111. /// cannot request a module analysis to actually run. Instead, the user must
  112. /// rely on the \c getCachedResult API.
  113. ///
  114. /// This proxy *doesn't* manage the invalidation in any way. That is handled by
  115. /// the recursive return path of each layer of the pass manager and the
  116. /// returned PreservedAnalysis set.
  117. class ModuleAnalysisManagerCGSCCProxy {
  118. public:
  119. /// \brief Result proxy object for \c ModuleAnalysisManagerCGSCCProxy.
  120. class Result {
  121. public:
  122. explicit Result(const ModuleAnalysisManager &MAM) : MAM(&MAM) {}
  123. // We have to explicitly define all the special member functions because
  124. // MSVC refuses to generate them.
  125. Result(const Result &Arg) : MAM(Arg.MAM) {}
  126. Result(Result &&Arg) : MAM(std::move(Arg.MAM)) {}
  127. Result &operator=(Result RHS) {
  128. std::swap(MAM, RHS.MAM);
  129. return *this;
  130. }
  131. const ModuleAnalysisManager &getManager() const { return *MAM; }
  132. /// \brief Handle invalidation by ignoring it, this pass is immutable.
  133. bool invalidate(LazyCallGraph::SCC &) { return false; }
  134. private:
  135. const ModuleAnalysisManager *MAM;
  136. };
  137. static void *ID() { return (void *)&PassID; }
  138. static StringRef name() { return "ModuleAnalysisManagerCGSCCProxy"; }
  139. ModuleAnalysisManagerCGSCCProxy(const ModuleAnalysisManager &MAM)
  140. : MAM(&MAM) {}
  141. // We have to explicitly define all the special member functions because MSVC
  142. // refuses to generate them.
  143. ModuleAnalysisManagerCGSCCProxy(const ModuleAnalysisManagerCGSCCProxy &Arg)
  144. : MAM(Arg.MAM) {}
  145. ModuleAnalysisManagerCGSCCProxy(ModuleAnalysisManagerCGSCCProxy &&Arg)
  146. : MAM(std::move(Arg.MAM)) {}
  147. ModuleAnalysisManagerCGSCCProxy &
  148. operator=(ModuleAnalysisManagerCGSCCProxy RHS) {
  149. std::swap(MAM, RHS.MAM);
  150. return *this;
  151. }
  152. /// \brief Run the analysis pass and create our proxy result object.
  153. /// Nothing to see here, it just forwards the \c MAM reference into the
  154. /// result.
  155. Result run(LazyCallGraph::SCC &) { return Result(*MAM); }
  156. private:
  157. static char PassID;
  158. const ModuleAnalysisManager *MAM;
  159. };
  160. /// \brief The core module pass which does a post-order walk of the SCCs and
  161. /// runs a CGSCC pass over each one.
  162. ///
  163. /// Designed to allow composition of a CGSCCPass(Manager) and
  164. /// a ModulePassManager. Note that this pass must be run with a module analysis
  165. /// manager as it uses the LazyCallGraph analysis. It will also run the
  166. /// \c CGSCCAnalysisManagerModuleProxy analysis prior to running the CGSCC
  167. /// pass over the module to enable a \c FunctionAnalysisManager to be used
  168. /// within this run safely.
  169. template <typename CGSCCPassT> class ModuleToPostOrderCGSCCPassAdaptor {
  170. public:
  171. explicit ModuleToPostOrderCGSCCPassAdaptor(CGSCCPassT Pass)
  172. : Pass(std::move(Pass)) {}
  173. // We have to explicitly define all the special member functions because MSVC
  174. // refuses to generate them.
  175. ModuleToPostOrderCGSCCPassAdaptor(
  176. const ModuleToPostOrderCGSCCPassAdaptor &Arg)
  177. : Pass(Arg.Pass) {}
  178. ModuleToPostOrderCGSCCPassAdaptor(ModuleToPostOrderCGSCCPassAdaptor &&Arg)
  179. : Pass(std::move(Arg.Pass)) {}
  180. friend void swap(ModuleToPostOrderCGSCCPassAdaptor &LHS,
  181. ModuleToPostOrderCGSCCPassAdaptor &RHS) {
  182. using std::swap;
  183. swap(LHS.Pass, RHS.Pass);
  184. }
  185. ModuleToPostOrderCGSCCPassAdaptor &
  186. operator=(ModuleToPostOrderCGSCCPassAdaptor RHS) {
  187. swap(*this, RHS);
  188. return *this;
  189. }
  190. /// \brief Runs the CGSCC pass across every SCC in the module.
  191. PreservedAnalyses run(Module &M, ModuleAnalysisManager *AM) {
  192. assert(AM && "We need analyses to compute the call graph!");
  193. // Setup the CGSCC analysis manager from its proxy.
  194. CGSCCAnalysisManager &CGAM =
  195. AM->getResult<CGSCCAnalysisManagerModuleProxy>(M).getManager();
  196. // Get the call graph for this module.
  197. LazyCallGraph &CG = AM->getResult<LazyCallGraphAnalysis>(M);
  198. PreservedAnalyses PA = PreservedAnalyses::all();
  199. for (LazyCallGraph::SCC &C : CG.postorder_sccs()) {
  200. PreservedAnalyses PassPA = Pass.run(C, &CGAM);
  201. // We know that the CGSCC pass couldn't have invalidated any other
  202. // SCC's analyses (that's the contract of a CGSCC pass), so
  203. // directly handle the CGSCC analysis manager's invalidation here. We
  204. // also update the preserved set of analyses to reflect that invalidated
  205. // analyses are now safe to preserve.
  206. // FIXME: This isn't quite correct. We need to handle the case where the
  207. // pass updated the CG, particularly some child of the current SCC, and
  208. // invalidate its analyses.
  209. PassPA = CGAM.invalidate(C, std::move(PassPA));
  210. // Then intersect the preserved set so that invalidation of module
  211. // analyses will eventually occur when the module pass completes.
  212. PA.intersect(std::move(PassPA));
  213. }
  214. // By definition we preserve the proxy. This precludes *any* invalidation
  215. // of CGSCC analyses by the proxy, but that's OK because we've taken
  216. // care to invalidate analyses in the CGSCC analysis manager
  217. // incrementally above.
  218. PA.preserve<CGSCCAnalysisManagerModuleProxy>();
  219. return PA;
  220. }
  221. static StringRef name() { return "ModuleToPostOrderCGSCCPassAdaptor"; }
  222. private:
  223. CGSCCPassT Pass;
  224. };
  225. /// \brief A function to deduce a function pass type and wrap it in the
  226. /// templated adaptor.
  227. template <typename CGSCCPassT>
  228. ModuleToPostOrderCGSCCPassAdaptor<CGSCCPassT>
  229. createModuleToPostOrderCGSCCPassAdaptor(CGSCCPassT Pass) {
  230. return ModuleToPostOrderCGSCCPassAdaptor<CGSCCPassT>(std::move(Pass));
  231. }
  232. /// \brief A CGSCC analysis which acts as a proxy for a function analysis
  233. /// manager.
  234. ///
  235. /// This primarily proxies invalidation information from the CGSCC analysis
  236. /// manager and CGSCC pass manager to a function analysis manager. You should
  237. /// never use a function analysis manager from within (transitively) a CGSCC
  238. /// pass manager unless your parent CGSCC pass has received a proxy result
  239. /// object for it.
  240. class FunctionAnalysisManagerCGSCCProxy {
  241. public:
  242. class Result {
  243. public:
  244. explicit Result(FunctionAnalysisManager &FAM) : FAM(&FAM) {}
  245. // We have to explicitly define all the special member functions because
  246. // MSVC refuses to generate them.
  247. Result(const Result &Arg) : FAM(Arg.FAM) {}
  248. Result(Result &&Arg) : FAM(std::move(Arg.FAM)) {}
  249. Result &operator=(Result RHS) {
  250. std::swap(FAM, RHS.FAM);
  251. return *this;
  252. }
  253. ~Result();
  254. /// \brief Accessor for the \c FunctionAnalysisManager.
  255. FunctionAnalysisManager &getManager() { return *FAM; }
  256. /// \brief Handler for invalidation of the SCC.
  257. ///
  258. /// If this analysis itself is preserved, then we assume that the set of \c
  259. /// Function objects in the \c SCC hasn't changed and thus we don't need
  260. /// to invalidate *all* cached data associated with a \c Function* in the \c
  261. /// FunctionAnalysisManager.
  262. ///
  263. /// Regardless of whether this analysis is marked as preserved, all of the
  264. /// analyses in the \c FunctionAnalysisManager are potentially invalidated
  265. /// based on the set of preserved analyses.
  266. bool invalidate(LazyCallGraph::SCC &C, const PreservedAnalyses &PA);
  267. private:
  268. FunctionAnalysisManager *FAM;
  269. };
  270. static void *ID() { return (void *)&PassID; }
  271. static StringRef name() { return "FunctionAnalysisManagerCGSCCProxy"; }
  272. explicit FunctionAnalysisManagerCGSCCProxy(FunctionAnalysisManager &FAM)
  273. : FAM(&FAM) {}
  274. // We have to explicitly define all the special member functions because MSVC
  275. // refuses to generate them.
  276. FunctionAnalysisManagerCGSCCProxy(
  277. const FunctionAnalysisManagerCGSCCProxy &Arg)
  278. : FAM(Arg.FAM) {}
  279. FunctionAnalysisManagerCGSCCProxy(FunctionAnalysisManagerCGSCCProxy &&Arg)
  280. : FAM(std::move(Arg.FAM)) {}
  281. FunctionAnalysisManagerCGSCCProxy &
  282. operator=(FunctionAnalysisManagerCGSCCProxy RHS) {
  283. std::swap(FAM, RHS.FAM);
  284. return *this;
  285. }
  286. /// \brief Run the analysis pass and create our proxy result object.
  287. ///
  288. /// This doesn't do any interesting work, it is primarily used to insert our
  289. /// proxy result object into the module analysis cache so that we can proxy
  290. /// invalidation to the function analysis manager.
  291. ///
  292. /// In debug builds, it will also assert that the analysis manager is empty
  293. /// as no queries should arrive at the function analysis manager prior to
  294. /// this analysis being requested.
  295. Result run(LazyCallGraph::SCC &C);
  296. private:
  297. static char PassID;
  298. FunctionAnalysisManager *FAM;
  299. };
  300. /// \brief A function analysis which acts as a proxy for a CGSCC analysis
  301. /// manager.
  302. ///
  303. /// This primarily provides an accessor to a parent CGSCC analysis manager to
  304. /// function passes. Only the const interface of the CGSCC analysis manager is
  305. /// provided to indicate that once inside of a function analysis pass you
  306. /// cannot request a CGSCC analysis to actually run. Instead, the user must
  307. /// rely on the \c getCachedResult API.
  308. ///
  309. /// This proxy *doesn't* manage the invalidation in any way. That is handled by
  310. /// the recursive return path of each layer of the pass manager and the
  311. /// returned PreservedAnalysis set.
  312. class CGSCCAnalysisManagerFunctionProxy {
  313. public:
  314. /// \brief Result proxy object for \c ModuleAnalysisManagerFunctionProxy.
  315. class Result {
  316. public:
  317. explicit Result(const CGSCCAnalysisManager &CGAM) : CGAM(&CGAM) {}
  318. // We have to explicitly define all the special member functions because
  319. // MSVC refuses to generate them.
  320. Result(const Result &Arg) : CGAM(Arg.CGAM) {}
  321. Result(Result &&Arg) : CGAM(std::move(Arg.CGAM)) {}
  322. Result &operator=(Result RHS) {
  323. std::swap(CGAM, RHS.CGAM);
  324. return *this;
  325. }
  326. const CGSCCAnalysisManager &getManager() const { return *CGAM; }
  327. /// \brief Handle invalidation by ignoring it, this pass is immutable.
  328. bool invalidate(Function &) { return false; }
  329. private:
  330. const CGSCCAnalysisManager *CGAM;
  331. };
  332. static void *ID() { return (void *)&PassID; }
  333. static StringRef name() { return "CGSCCAnalysisManagerFunctionProxy"; }
  334. CGSCCAnalysisManagerFunctionProxy(const CGSCCAnalysisManager &CGAM)
  335. : CGAM(&CGAM) {}
  336. // We have to explicitly define all the special member functions because MSVC
  337. // refuses to generate them.
  338. CGSCCAnalysisManagerFunctionProxy(
  339. const CGSCCAnalysisManagerFunctionProxy &Arg)
  340. : CGAM(Arg.CGAM) {}
  341. CGSCCAnalysisManagerFunctionProxy(CGSCCAnalysisManagerFunctionProxy &&Arg)
  342. : CGAM(std::move(Arg.CGAM)) {}
  343. CGSCCAnalysisManagerFunctionProxy &
  344. operator=(CGSCCAnalysisManagerFunctionProxy RHS) {
  345. std::swap(CGAM, RHS.CGAM);
  346. return *this;
  347. }
  348. /// \brief Run the analysis pass and create our proxy result object.
  349. /// Nothing to see here, it just forwards the \c CGAM reference into the
  350. /// result.
  351. Result run(Function &) { return Result(*CGAM); }
  352. private:
  353. static char PassID;
  354. const CGSCCAnalysisManager *CGAM;
  355. };
  356. /// \brief Adaptor that maps from a SCC to its functions.
  357. ///
  358. /// Designed to allow composition of a FunctionPass(Manager) and
  359. /// a CGSCCPassManager. Note that if this pass is constructed with a pointer
  360. /// to a \c CGSCCAnalysisManager it will run the
  361. /// \c FunctionAnalysisManagerCGSCCProxy analysis prior to running the function
  362. /// pass over the SCC to enable a \c FunctionAnalysisManager to be used
  363. /// within this run safely.
  364. template <typename FunctionPassT> class CGSCCToFunctionPassAdaptor {
  365. public:
  366. explicit CGSCCToFunctionPassAdaptor(FunctionPassT Pass)
  367. : Pass(std::move(Pass)) {}
  368. // We have to explicitly define all the special member functions because MSVC
  369. // refuses to generate them.
  370. CGSCCToFunctionPassAdaptor(const CGSCCToFunctionPassAdaptor &Arg)
  371. : Pass(Arg.Pass) {}
  372. CGSCCToFunctionPassAdaptor(CGSCCToFunctionPassAdaptor &&Arg)
  373. : Pass(std::move(Arg.Pass)) {}
  374. friend void swap(CGSCCToFunctionPassAdaptor &LHS,
  375. CGSCCToFunctionPassAdaptor &RHS) {
  376. using std::swap;
  377. swap(LHS.Pass, RHS.Pass);
  378. }
  379. CGSCCToFunctionPassAdaptor &operator=(CGSCCToFunctionPassAdaptor RHS) {
  380. swap(*this, RHS);
  381. return *this;
  382. }
  383. /// \brief Runs the function pass across every function in the module.
  384. PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager *AM) {
  385. FunctionAnalysisManager *FAM = nullptr;
  386. if (AM)
  387. // Setup the function analysis manager from its proxy.
  388. FAM = &AM->getResult<FunctionAnalysisManagerCGSCCProxy>(C).getManager();
  389. PreservedAnalyses PA = PreservedAnalyses::all();
  390. for (LazyCallGraph::Node *N : C) {
  391. PreservedAnalyses PassPA = Pass.run(N->getFunction(), FAM);
  392. // We know that the function pass couldn't have invalidated any other
  393. // function's analyses (that's the contract of a function pass), so
  394. // directly handle the function analysis manager's invalidation here.
  395. // Also, update the preserved analyses to reflect that once invalidated
  396. // these can again be preserved.
  397. if (FAM)
  398. PassPA = FAM->invalidate(N->getFunction(), std::move(PassPA));
  399. // Then intersect the preserved set so that invalidation of module
  400. // analyses will eventually occur when the module pass completes.
  401. PA.intersect(std::move(PassPA));
  402. }
  403. // By definition we preserve the proxy. This precludes *any* invalidation
  404. // of function analyses by the proxy, but that's OK because we've taken
  405. // care to invalidate analyses in the function analysis manager
  406. // incrementally above.
  407. // FIXME: We need to update the call graph here to account for any deleted
  408. // edges!
  409. PA.preserve<FunctionAnalysisManagerCGSCCProxy>();
  410. return PA;
  411. }
  412. static StringRef name() { return "CGSCCToFunctionPassAdaptor"; }
  413. private:
  414. FunctionPassT Pass;
  415. };
  416. /// \brief A function to deduce a function pass type and wrap it in the
  417. /// templated adaptor.
  418. template <typename FunctionPassT>
  419. CGSCCToFunctionPassAdaptor<FunctionPassT>
  420. createCGSCCToFunctionPassAdaptor(FunctionPassT Pass) {
  421. return CGSCCToFunctionPassAdaptor<FunctionPassT>(std::move(Pass));
  422. }
  423. }
  424. #endif