LoopPassManager.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- LoopPassManager.h - Loop pass management -----------------*- 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 header provides classes for managing a pipeline of passes over loops
  16. /// in LLVM IR.
  17. ///
  18. /// The primary loop pass pipeline is managed in a very particular way to
  19. /// provide a set of core guarantees:
  20. /// 1) Loops are, where possible, in simplified form.
  21. /// 2) Loops are *always* in LCSSA form.
  22. /// 3) A collection of Loop-specific analysis results are available:
  23. /// - LoopInfo
  24. /// - DominatorTree
  25. /// - ScalarEvolution
  26. /// - AAManager
  27. /// 4) All loop passes preserve #1 (where possible), #2, and #3.
  28. /// 5) Loop passes run over each loop in the loop nest from the innermost to
  29. /// the outermost. Specifically, all inner loops are processed before
  30. /// passes run over outer loops. When running the pipeline across an inner
  31. /// loop creates new inner loops, those are added and processed in this
  32. /// order as well.
  33. ///
  34. /// This process is designed to facilitate transformations which simplify,
  35. /// reduce, and remove loops. For passes which are more oriented towards
  36. /// optimizing loops, especially optimizing loop *nests* instead of single
  37. /// loops in isolation, this framework is less interesting.
  38. ///
  39. //===----------------------------------------------------------------------===//
  40. #ifndef LLVM_TRANSFORMS_SCALAR_LOOPPASSMANAGER_H
  41. #define LLVM_TRANSFORMS_SCALAR_LOOPPASSMANAGER_H
  42. #include "llvm/ADT/PriorityWorklist.h"
  43. #include "llvm/Analysis/LoopAnalysisManager.h"
  44. #include "llvm/Analysis/LoopInfo.h"
  45. #include "llvm/Analysis/LoopNestAnalysis.h"
  46. #include "llvm/IR/Dominators.h"
  47. #include "llvm/IR/PassInstrumentation.h"
  48. #include "llvm/IR/PassManager.h"
  49. #include "llvm/Transforms/Utils/LCSSA.h"
  50. #include "llvm/Transforms/Utils/LoopSimplify.h"
  51. #include "llvm/Transforms/Utils/LoopUtils.h"
  52. #include <memory>
  53. namespace llvm {
  54. // Forward declarations of an update tracking API used in the pass manager.
  55. class LPMUpdater;
  56. namespace {
  57. template <typename PassT>
  58. using HasRunOnLoopT = decltype(std::declval<PassT>().run(
  59. std::declval<Loop &>(), std::declval<LoopAnalysisManager &>(),
  60. std::declval<LoopStandardAnalysisResults &>(),
  61. std::declval<LPMUpdater &>()));
  62. } // namespace
  63. // Explicit specialization and instantiation declarations for the pass manager.
  64. // See the comments on the definition of the specialization for details on how
  65. // it differs from the primary template.
  66. template <>
  67. class PassManager<Loop, LoopAnalysisManager, LoopStandardAnalysisResults &,
  68. LPMUpdater &>
  69. : public PassInfoMixin<
  70. PassManager<Loop, LoopAnalysisManager, LoopStandardAnalysisResults &,
  71. LPMUpdater &>> {
  72. public:
  73. explicit PassManager() = default;
  74. // FIXME: These are equivalent to the default move constructor/move
  75. // assignment. However, using = default triggers linker errors due to the
  76. // explicit instantiations below. Find a way to use the default and remove the
  77. // duplicated code here.
  78. PassManager(PassManager &&Arg)
  79. : IsLoopNestPass(std::move(Arg.IsLoopNestPass)),
  80. LoopPasses(std::move(Arg.LoopPasses)),
  81. LoopNestPasses(std::move(Arg.LoopNestPasses)) {}
  82. PassManager &operator=(PassManager &&RHS) {
  83. IsLoopNestPass = std::move(RHS.IsLoopNestPass);
  84. LoopPasses = std::move(RHS.LoopPasses);
  85. LoopNestPasses = std::move(RHS.LoopNestPasses);
  86. return *this;
  87. }
  88. PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
  89. LoopStandardAnalysisResults &AR, LPMUpdater &U);
  90. void printPipeline(raw_ostream &OS,
  91. function_ref<StringRef(StringRef)> MapClassName2PassName);
  92. /// Add either a loop pass or a loop-nest pass to the pass manager. Append \p
  93. /// Pass to the list of loop passes if it has a dedicated \fn run() method for
  94. /// loops and to the list of loop-nest passes if the \fn run() method is for
  95. /// loop-nests instead. Also append whether \p Pass is loop-nest pass or not
  96. /// to the end of \var IsLoopNestPass so we can easily identify the types of
  97. /// passes in the pass manager later.
  98. template <typename PassT>
  99. LLVM_ATTRIBUTE_MINSIZE
  100. std::enable_if_t<is_detected<HasRunOnLoopT, PassT>::value>
  101. addPass(PassT &&Pass) {
  102. using LoopPassModelT =
  103. detail::PassModel<Loop, PassT, PreservedAnalyses, LoopAnalysisManager,
  104. LoopStandardAnalysisResults &, LPMUpdater &>;
  105. IsLoopNestPass.push_back(false);
  106. // Do not use make_unique or emplace_back, they cause too many template
  107. // instantiations, causing terrible compile times.
  108. LoopPasses.push_back(std::unique_ptr<LoopPassConceptT>(
  109. new LoopPassModelT(std::forward<PassT>(Pass))));
  110. }
  111. template <typename PassT>
  112. LLVM_ATTRIBUTE_MINSIZE
  113. std::enable_if_t<!is_detected<HasRunOnLoopT, PassT>::value>
  114. addPass(PassT &&Pass) {
  115. using LoopNestPassModelT =
  116. detail::PassModel<LoopNest, PassT, PreservedAnalyses,
  117. LoopAnalysisManager, LoopStandardAnalysisResults &,
  118. LPMUpdater &>;
  119. IsLoopNestPass.push_back(true);
  120. // Do not use make_unique or emplace_back, they cause too many template
  121. // instantiations, causing terrible compile times.
  122. LoopNestPasses.push_back(std::unique_ptr<LoopNestPassConceptT>(
  123. new LoopNestPassModelT(std::forward<PassT>(Pass))));
  124. }
  125. // Specializations of `addPass` for `RepeatedPass`. These are necessary since
  126. // `RepeatedPass` has a templated `run` method that will result in incorrect
  127. // detection of `HasRunOnLoopT`.
  128. template <typename PassT>
  129. LLVM_ATTRIBUTE_MINSIZE
  130. std::enable_if_t<is_detected<HasRunOnLoopT, PassT>::value>
  131. addPass(RepeatedPass<PassT> &&Pass) {
  132. using RepeatedLoopPassModelT =
  133. detail::PassModel<Loop, RepeatedPass<PassT>, PreservedAnalyses,
  134. LoopAnalysisManager, LoopStandardAnalysisResults &,
  135. LPMUpdater &>;
  136. IsLoopNestPass.push_back(false);
  137. // Do not use make_unique or emplace_back, they cause too many template
  138. // instantiations, causing terrible compile times.
  139. LoopPasses.push_back(std::unique_ptr<LoopPassConceptT>(
  140. new RepeatedLoopPassModelT(std::move(Pass))));
  141. }
  142. template <typename PassT>
  143. LLVM_ATTRIBUTE_MINSIZE
  144. std::enable_if_t<!is_detected<HasRunOnLoopT, PassT>::value>
  145. addPass(RepeatedPass<PassT> &&Pass) {
  146. using RepeatedLoopNestPassModelT =
  147. detail::PassModel<LoopNest, RepeatedPass<PassT>, PreservedAnalyses,
  148. LoopAnalysisManager, LoopStandardAnalysisResults &,
  149. LPMUpdater &>;
  150. IsLoopNestPass.push_back(true);
  151. // Do not use make_unique or emplace_back, they cause too many template
  152. // instantiations, causing terrible compile times.
  153. LoopNestPasses.push_back(std::unique_ptr<LoopNestPassConceptT>(
  154. new RepeatedLoopNestPassModelT(std::move(Pass))));
  155. }
  156. bool isEmpty() const { return LoopPasses.empty() && LoopNestPasses.empty(); }
  157. static bool isRequired() { return true; }
  158. size_t getNumLoopPasses() const { return LoopPasses.size(); }
  159. size_t getNumLoopNestPasses() const { return LoopNestPasses.size(); }
  160. protected:
  161. using LoopPassConceptT =
  162. detail::PassConcept<Loop, LoopAnalysisManager,
  163. LoopStandardAnalysisResults &, LPMUpdater &>;
  164. using LoopNestPassConceptT =
  165. detail::PassConcept<LoopNest, LoopAnalysisManager,
  166. LoopStandardAnalysisResults &, LPMUpdater &>;
  167. // BitVector that identifies whether the passes are loop passes or loop-nest
  168. // passes (true for loop-nest passes).
  169. BitVector IsLoopNestPass;
  170. std::vector<std::unique_ptr<LoopPassConceptT>> LoopPasses;
  171. std::vector<std::unique_ptr<LoopNestPassConceptT>> LoopNestPasses;
  172. /// Run either a loop pass or a loop-nest pass. Returns `None` if
  173. /// PassInstrumentation's BeforePass returns false. Otherwise, returns the
  174. /// preserved analyses of the pass.
  175. template <typename IRUnitT, typename PassT>
  176. Optional<PreservedAnalyses>
  177. runSinglePass(IRUnitT &IR, PassT &Pass, LoopAnalysisManager &AM,
  178. LoopStandardAnalysisResults &AR, LPMUpdater &U,
  179. PassInstrumentation &PI);
  180. PreservedAnalyses runWithLoopNestPasses(Loop &L, LoopAnalysisManager &AM,
  181. LoopStandardAnalysisResults &AR,
  182. LPMUpdater &U);
  183. PreservedAnalyses runWithoutLoopNestPasses(Loop &L, LoopAnalysisManager &AM,
  184. LoopStandardAnalysisResults &AR,
  185. LPMUpdater &U);
  186. private:
  187. static const Loop &getLoopFromIR(Loop &L) { return L; }
  188. static const Loop &getLoopFromIR(LoopNest &LN) {
  189. return LN.getOutermostLoop();
  190. }
  191. };
  192. /// The Loop pass manager.
  193. ///
  194. /// See the documentation for the PassManager template for details. It runs
  195. /// a sequence of Loop passes over each Loop that the manager is run over. This
  196. /// typedef serves as a convenient way to refer to this construct.
  197. typedef PassManager<Loop, LoopAnalysisManager, LoopStandardAnalysisResults &,
  198. LPMUpdater &>
  199. LoopPassManager;
  200. /// A partial specialization of the require analysis template pass to forward
  201. /// the extra parameters from a transformation's run method to the
  202. /// AnalysisManager's getResult.
  203. template <typename AnalysisT>
  204. struct RequireAnalysisPass<AnalysisT, Loop, LoopAnalysisManager,
  205. LoopStandardAnalysisResults &, LPMUpdater &>
  206. : PassInfoMixin<
  207. RequireAnalysisPass<AnalysisT, Loop, LoopAnalysisManager,
  208. LoopStandardAnalysisResults &, LPMUpdater &>> {
  209. PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
  210. LoopStandardAnalysisResults &AR, LPMUpdater &) {
  211. (void)AM.template getResult<AnalysisT>(L, AR);
  212. return PreservedAnalyses::all();
  213. }
  214. void printPipeline(raw_ostream &OS,
  215. function_ref<StringRef(StringRef)> MapClassName2PassName) {
  216. auto ClassName = AnalysisT::name();
  217. auto PassName = MapClassName2PassName(ClassName);
  218. OS << "require<" << PassName << ">";
  219. }
  220. };
  221. /// An alias template to easily name a require analysis loop pass.
  222. template <typename AnalysisT>
  223. using RequireAnalysisLoopPass =
  224. RequireAnalysisPass<AnalysisT, Loop, LoopAnalysisManager,
  225. LoopStandardAnalysisResults &, LPMUpdater &>;
  226. class FunctionToLoopPassAdaptor;
  227. /// This class provides an interface for updating the loop pass manager based
  228. /// on mutations to the loop nest.
  229. ///
  230. /// A reference to an instance of this class is passed as an argument to each
  231. /// Loop pass, and Loop passes should use it to update LPM infrastructure if
  232. /// they modify the loop nest structure.
  233. ///
  234. /// \c LPMUpdater comes with two modes: the loop mode and the loop-nest mode. In
  235. /// loop mode, all the loops in the function will be pushed into the worklist
  236. /// and when new loops are added to the pipeline, their subloops are also
  237. /// inserted recursively. On the other hand, in loop-nest mode, only top-level
  238. /// loops are contained in the worklist and the addition of new (top-level)
  239. /// loops will not trigger the addition of their subloops.
  240. class LPMUpdater {
  241. public:
  242. /// This can be queried by loop passes which run other loop passes (like pass
  243. /// managers) to know whether the loop needs to be skipped due to updates to
  244. /// the loop nest.
  245. ///
  246. /// If this returns true, the loop object may have been deleted, so passes
  247. /// should take care not to touch the object.
  248. bool skipCurrentLoop() const { return SkipCurrentLoop; }
  249. /// Loop passes should use this method to indicate they have deleted a loop
  250. /// from the nest.
  251. ///
  252. /// Note that this loop must either be the current loop or a subloop of the
  253. /// current loop. This routine must be called prior to removing the loop from
  254. /// the loop nest.
  255. ///
  256. /// If this is called for the current loop, in addition to clearing any
  257. /// state, this routine will mark that the current loop should be skipped by
  258. /// the rest of the pass management infrastructure.
  259. void markLoopAsDeleted(Loop &L, llvm::StringRef Name) {
  260. LAM.clear(L, Name);
  261. assert((&L == CurrentL || CurrentL->contains(&L)) &&
  262. "Cannot delete a loop outside of the "
  263. "subloop tree currently being processed.");
  264. if (&L == CurrentL)
  265. SkipCurrentLoop = true;
  266. }
  267. void setParentLoop(Loop *L) {
  268. #ifdef LLVM_ENABLE_ABI_BREAKING_CHECKS
  269. ParentL = L;
  270. #endif
  271. }
  272. /// Loop passes should use this method to indicate they have added new child
  273. /// loops of the current loop.
  274. ///
  275. /// \p NewChildLoops must contain only the immediate children. Any nested
  276. /// loops within them will be visited in postorder as usual for the loop pass
  277. /// manager.
  278. void addChildLoops(ArrayRef<Loop *> NewChildLoops) {
  279. assert(!LoopNestMode &&
  280. "Child loops should not be pushed in loop-nest mode.");
  281. // Insert ourselves back into the worklist first, as this loop should be
  282. // revisited after all the children have been processed.
  283. Worklist.insert(CurrentL);
  284. #ifndef NDEBUG
  285. for (Loop *NewL : NewChildLoops)
  286. assert(NewL->getParentLoop() == CurrentL && "All of the new loops must "
  287. "be immediate children of "
  288. "the current loop!");
  289. #endif
  290. appendLoopsToWorklist(NewChildLoops, Worklist);
  291. // Also skip further processing of the current loop--it will be revisited
  292. // after all of its newly added children are accounted for.
  293. SkipCurrentLoop = true;
  294. }
  295. /// Loop passes should use this method to indicate they have added new
  296. /// sibling loops to the current loop.
  297. ///
  298. /// \p NewSibLoops must only contain the immediate sibling loops. Any nested
  299. /// loops within them will be visited in postorder as usual for the loop pass
  300. /// manager.
  301. void addSiblingLoops(ArrayRef<Loop *> NewSibLoops) {
  302. #if defined(LLVM_ENABLE_ABI_BREAKING_CHECKS) && !defined(NDEBUG)
  303. for (Loop *NewL : NewSibLoops)
  304. assert(NewL->getParentLoop() == ParentL &&
  305. "All of the new loops must be siblings of the current loop!");
  306. #endif
  307. if (LoopNestMode)
  308. Worklist.insert(NewSibLoops);
  309. else
  310. appendLoopsToWorklist(NewSibLoops, Worklist);
  311. // No need to skip the current loop or revisit it, as sibling loops
  312. // shouldn't impact anything.
  313. }
  314. /// Restart the current loop.
  315. ///
  316. /// Loop passes should call this method to indicate the current loop has been
  317. /// sufficiently changed that it should be re-visited from the begining of
  318. /// the loop pass pipeline rather than continuing.
  319. void revisitCurrentLoop() {
  320. // Tell the currently in-flight pipeline to stop running.
  321. SkipCurrentLoop = true;
  322. // And insert ourselves back into the worklist.
  323. Worklist.insert(CurrentL);
  324. }
  325. private:
  326. friend class llvm::FunctionToLoopPassAdaptor;
  327. /// The \c FunctionToLoopPassAdaptor's worklist of loops to process.
  328. SmallPriorityWorklist<Loop *, 4> &Worklist;
  329. /// The analysis manager for use in the current loop nest.
  330. LoopAnalysisManager &LAM;
  331. Loop *CurrentL;
  332. bool SkipCurrentLoop;
  333. const bool LoopNestMode;
  334. #ifdef LLVM_ENABLE_ABI_BREAKING_CHECKS
  335. // In debug builds we also track the parent loop to implement asserts even in
  336. // the face of loop deletion.
  337. Loop *ParentL;
  338. #endif
  339. LPMUpdater(SmallPriorityWorklist<Loop *, 4> &Worklist,
  340. LoopAnalysisManager &LAM, bool LoopNestMode = false)
  341. : Worklist(Worklist), LAM(LAM), LoopNestMode(LoopNestMode) {}
  342. };
  343. template <typename IRUnitT, typename PassT>
  344. Optional<PreservedAnalyses> LoopPassManager::runSinglePass(
  345. IRUnitT &IR, PassT &Pass, LoopAnalysisManager &AM,
  346. LoopStandardAnalysisResults &AR, LPMUpdater &U, PassInstrumentation &PI) {
  347. // Get the loop in case of Loop pass and outermost loop in case of LoopNest
  348. // pass which is to be passed to BeforePass and AfterPass call backs.
  349. const Loop &L = getLoopFromIR(IR);
  350. // Check the PassInstrumentation's BeforePass callbacks before running the
  351. // pass, skip its execution completely if asked to (callback returns false).
  352. if (!PI.runBeforePass<Loop>(*Pass, L))
  353. return None;
  354. PreservedAnalyses PA;
  355. {
  356. TimeTraceScope TimeScope(Pass->name(), IR.getName());
  357. PA = Pass->run(IR, AM, AR, U);
  358. }
  359. // do not pass deleted Loop into the instrumentation
  360. if (U.skipCurrentLoop())
  361. PI.runAfterPassInvalidated<IRUnitT>(*Pass, PA);
  362. else
  363. PI.runAfterPass<Loop>(*Pass, L, PA);
  364. return PA;
  365. }
  366. /// Adaptor that maps from a function to its loops.
  367. ///
  368. /// Designed to allow composition of a LoopPass(Manager) and a
  369. /// FunctionPassManager. Note that if this pass is constructed with a \c
  370. /// FunctionAnalysisManager it will run the \c LoopAnalysisManagerFunctionProxy
  371. /// analysis prior to running the loop passes over the function to enable a \c
  372. /// LoopAnalysisManager to be used within this run safely.
  373. ///
  374. /// The adaptor comes with two modes: the loop mode and the loop-nest mode, and
  375. /// the worklist updater lived inside will be in the same mode as the adaptor
  376. /// (refer to the documentation of \c LPMUpdater for more detailed explanation).
  377. /// Specifically, in loop mode, all loops in the funciton will be pushed into
  378. /// the worklist and processed by \p Pass, while only top-level loops are
  379. /// processed in loop-nest mode. Please refer to the various specializations of
  380. /// \fn createLoopFunctionToLoopPassAdaptor to see when loop mode and loop-nest
  381. /// mode are used.
  382. class FunctionToLoopPassAdaptor
  383. : public PassInfoMixin<FunctionToLoopPassAdaptor> {
  384. public:
  385. using PassConceptT =
  386. detail::PassConcept<Loop, LoopAnalysisManager,
  387. LoopStandardAnalysisResults &, LPMUpdater &>;
  388. explicit FunctionToLoopPassAdaptor(std::unique_ptr<PassConceptT> Pass,
  389. bool UseMemorySSA = false,
  390. bool UseBlockFrequencyInfo = false,
  391. bool UseBranchProbabilityInfo = false,
  392. bool LoopNestMode = false)
  393. : Pass(std::move(Pass)), UseMemorySSA(UseMemorySSA),
  394. UseBlockFrequencyInfo(UseBlockFrequencyInfo),
  395. UseBranchProbabilityInfo(UseBranchProbabilityInfo),
  396. LoopNestMode(LoopNestMode) {
  397. LoopCanonicalizationFPM.addPass(LoopSimplifyPass());
  398. LoopCanonicalizationFPM.addPass(LCSSAPass());
  399. }
  400. /// Runs the loop passes across every loop in the function.
  401. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  402. void printPipeline(raw_ostream &OS,
  403. function_ref<StringRef(StringRef)> MapClassName2PassName);
  404. static bool isRequired() { return true; }
  405. bool isLoopNestMode() const { return LoopNestMode; }
  406. private:
  407. std::unique_ptr<PassConceptT> Pass;
  408. FunctionPassManager LoopCanonicalizationFPM;
  409. bool UseMemorySSA = false;
  410. bool UseBlockFrequencyInfo = false;
  411. bool UseBranchProbabilityInfo = false;
  412. const bool LoopNestMode;
  413. };
  414. /// A function to deduce a loop pass type and wrap it in the templated
  415. /// adaptor.
  416. ///
  417. /// If \p Pass is a loop pass, the returned adaptor will be in loop mode.
  418. template <typename LoopPassT>
  419. inline std::enable_if_t<is_detected<HasRunOnLoopT, LoopPassT>::value,
  420. FunctionToLoopPassAdaptor>
  421. createFunctionToLoopPassAdaptor(LoopPassT &&Pass, bool UseMemorySSA = false,
  422. bool UseBlockFrequencyInfo = false,
  423. bool UseBranchProbabilityInfo = false) {
  424. using PassModelT =
  425. detail::PassModel<Loop, LoopPassT, PreservedAnalyses, LoopAnalysisManager,
  426. LoopStandardAnalysisResults &, LPMUpdater &>;
  427. // Do not use make_unique, it causes too many template instantiations,
  428. // causing terrible compile times.
  429. return FunctionToLoopPassAdaptor(
  430. std::unique_ptr<FunctionToLoopPassAdaptor::PassConceptT>(
  431. new PassModelT(std::forward<LoopPassT>(Pass))),
  432. UseMemorySSA, UseBlockFrequencyInfo, UseBranchProbabilityInfo, false);
  433. }
  434. /// If \p Pass is a loop-nest pass, \p Pass will first be wrapped into a
  435. /// \c LoopPassManager and the returned adaptor will be in loop-nest mode.
  436. template <typename LoopNestPassT>
  437. inline std::enable_if_t<!is_detected<HasRunOnLoopT, LoopNestPassT>::value,
  438. FunctionToLoopPassAdaptor>
  439. createFunctionToLoopPassAdaptor(LoopNestPassT &&Pass, bool UseMemorySSA = false,
  440. bool UseBlockFrequencyInfo = false,
  441. bool UseBranchProbabilityInfo = false) {
  442. LoopPassManager LPM;
  443. LPM.addPass(std::forward<LoopNestPassT>(Pass));
  444. using PassModelT =
  445. detail::PassModel<Loop, LoopPassManager, PreservedAnalyses,
  446. LoopAnalysisManager, LoopStandardAnalysisResults &,
  447. LPMUpdater &>;
  448. // Do not use make_unique, it causes too many template instantiations,
  449. // causing terrible compile times.
  450. return FunctionToLoopPassAdaptor(
  451. std::unique_ptr<FunctionToLoopPassAdaptor::PassConceptT>(
  452. new PassModelT(std::move(LPM))),
  453. UseMemorySSA, UseBlockFrequencyInfo, UseBranchProbabilityInfo, true);
  454. }
  455. /// If \p Pass is an instance of \c LoopPassManager, the returned adaptor will
  456. /// be in loop-nest mode if the pass manager contains only loop-nest passes.
  457. template <>
  458. inline FunctionToLoopPassAdaptor
  459. createFunctionToLoopPassAdaptor<LoopPassManager>(
  460. LoopPassManager &&LPM, bool UseMemorySSA, bool UseBlockFrequencyInfo,
  461. bool UseBranchProbabilityInfo) {
  462. // Check if LPM contains any loop pass and if it does not, returns an adaptor
  463. // in loop-nest mode.
  464. using PassModelT =
  465. detail::PassModel<Loop, LoopPassManager, PreservedAnalyses,
  466. LoopAnalysisManager, LoopStandardAnalysisResults &,
  467. LPMUpdater &>;
  468. bool LoopNestMode = (LPM.getNumLoopPasses() == 0);
  469. // Do not use make_unique, it causes too many template instantiations,
  470. // causing terrible compile times.
  471. return FunctionToLoopPassAdaptor(
  472. std::unique_ptr<FunctionToLoopPassAdaptor::PassConceptT>(
  473. new PassModelT(std::move(LPM))),
  474. UseMemorySSA, UseBlockFrequencyInfo, UseBranchProbabilityInfo,
  475. LoopNestMode);
  476. }
  477. /// Pass for printing a loop's contents as textual IR.
  478. class PrintLoopPass : public PassInfoMixin<PrintLoopPass> {
  479. raw_ostream &OS;
  480. std::string Banner;
  481. public:
  482. PrintLoopPass();
  483. PrintLoopPass(raw_ostream &OS, const std::string &Banner = "");
  484. PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
  485. LoopStandardAnalysisResults &, LPMUpdater &);
  486. };
  487. }
  488. #endif // LLVM_TRANSFORMS_SCALAR_LOOPPASSMANAGER_H
  489. #ifdef __GNUC__
  490. #pragma GCC diagnostic pop
  491. #endif