LoopAnalysisManager.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- LoopAnalysisManager.h - Loop analysis 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 per-loop analyses. These are
  16. /// typically used as part of a loop pass pipeline over the loop nests of
  17. /// a function.
  18. ///
  19. /// Loop analyses are allowed to make some simplifying assumptions:
  20. /// 1) Loops are, where possible, in simplified form.
  21. /// 2) Loops are *always* in LCSSA form.
  22. /// 3) A collection of analysis results are available:
  23. /// - LoopInfo
  24. /// - DominatorTree
  25. /// - ScalarEvolution
  26. /// - AAManager
  27. ///
  28. /// The primary mechanism to provide these invariants is the loop pass manager,
  29. /// but they can also be manually provided in order to reason about a loop from
  30. /// outside of a dedicated pass manager.
  31. ///
  32. //===----------------------------------------------------------------------===//
  33. #ifndef LLVM_ANALYSIS_LOOPANALYSISMANAGER_H
  34. #define LLVM_ANALYSIS_LOOPANALYSISMANAGER_H
  35. #include "llvm/ADT/PostOrderIterator.h"
  36. #include "llvm/IR/PassManager.h"
  37. namespace llvm {
  38. class AAResults;
  39. class AssumptionCache;
  40. class DominatorTree;
  41. class Function;
  42. class Loop;
  43. class LoopInfo;
  44. class MemorySSA;
  45. class ScalarEvolution;
  46. class TargetLibraryInfo;
  47. class TargetTransformInfo;
  48. /// The adaptor from a function pass to a loop pass computes these analyses and
  49. /// makes them available to the loop passes "for free". Each loop pass is
  50. /// expected to update these analyses if necessary to ensure they're
  51. /// valid after it runs.
  52. struct LoopStandardAnalysisResults {
  53. AAResults &AA;
  54. AssumptionCache ∾
  55. DominatorTree &DT;
  56. LoopInfo &LI;
  57. ScalarEvolution &SE;
  58. TargetLibraryInfo &TLI;
  59. TargetTransformInfo &TTI;
  60. BlockFrequencyInfo *BFI;
  61. BranchProbabilityInfo *BPI;
  62. MemorySSA *MSSA;
  63. };
  64. /// Extern template declaration for the analysis set for this IR unit.
  65. extern template class AllAnalysesOn<Loop>;
  66. extern template class AnalysisManager<Loop, LoopStandardAnalysisResults &>;
  67. /// The loop analysis manager.
  68. ///
  69. /// See the documentation for the AnalysisManager template for detail
  70. /// documentation. This typedef serves as a convenient way to refer to this
  71. /// construct in the adaptors and proxies used to integrate this into the larger
  72. /// pass manager infrastructure.
  73. typedef AnalysisManager<Loop, LoopStandardAnalysisResults &>
  74. LoopAnalysisManager;
  75. /// A proxy from a \c LoopAnalysisManager to a \c Function.
  76. typedef InnerAnalysisManagerProxy<LoopAnalysisManager, Function>
  77. LoopAnalysisManagerFunctionProxy;
  78. /// A specialized result for the \c LoopAnalysisManagerFunctionProxy which
  79. /// retains a \c LoopInfo reference.
  80. ///
  81. /// This allows it to collect loop objects for which analysis results may be
  82. /// cached in the \c LoopAnalysisManager.
  83. template <> class LoopAnalysisManagerFunctionProxy::Result {
  84. public:
  85. explicit Result(LoopAnalysisManager &InnerAM, LoopInfo &LI)
  86. : InnerAM(&InnerAM), LI(&LI) {}
  87. Result(Result &&Arg)
  88. : InnerAM(std::move(Arg.InnerAM)), LI(Arg.LI), MSSAUsed(Arg.MSSAUsed) {
  89. // We have to null out the analysis manager in the moved-from state
  90. // because we are taking ownership of the responsibilty to clear the
  91. // analysis state.
  92. Arg.InnerAM = nullptr;
  93. }
  94. Result &operator=(Result &&RHS) {
  95. InnerAM = RHS.InnerAM;
  96. LI = RHS.LI;
  97. MSSAUsed = RHS.MSSAUsed;
  98. // We have to null out the analysis manager in the moved-from state
  99. // because we are taking ownership of the responsibilty to clear the
  100. // analysis state.
  101. RHS.InnerAM = nullptr;
  102. return *this;
  103. }
  104. ~Result() {
  105. // InnerAM is cleared in a moved from state where there is nothing to do.
  106. if (!InnerAM)
  107. return;
  108. // Clear out the analysis manager if we're being destroyed -- it means we
  109. // didn't even see an invalidate call when we got invalidated.
  110. InnerAM->clear();
  111. }
  112. /// Mark MemorySSA as used so we can invalidate self if MSSA is invalidated.
  113. void markMSSAUsed() { MSSAUsed = true; }
  114. /// Accessor for the analysis manager.
  115. LoopAnalysisManager &getManager() { return *InnerAM; }
  116. /// Handler for invalidation of the proxy for a particular function.
  117. ///
  118. /// If the proxy, \c LoopInfo, and associated analyses are preserved, this
  119. /// will merely forward the invalidation event to any cached loop analysis
  120. /// results for loops within this function.
  121. ///
  122. /// If the necessary loop infrastructure is not preserved, this will forcibly
  123. /// clear all of the cached analysis results that are keyed on the \c
  124. /// LoopInfo for this function.
  125. bool invalidate(Function &F, const PreservedAnalyses &PA,
  126. FunctionAnalysisManager::Invalidator &Inv);
  127. private:
  128. LoopAnalysisManager *InnerAM;
  129. LoopInfo *LI;
  130. bool MSSAUsed = false;
  131. };
  132. /// Provide a specialized run method for the \c LoopAnalysisManagerFunctionProxy
  133. /// so it can pass the \c LoopInfo to the result.
  134. template <>
  135. LoopAnalysisManagerFunctionProxy::Result
  136. LoopAnalysisManagerFunctionProxy::run(Function &F, FunctionAnalysisManager &AM);
  137. // Ensure the \c LoopAnalysisManagerFunctionProxy is provided as an extern
  138. // template.
  139. extern template class InnerAnalysisManagerProxy<LoopAnalysisManager, Function>;
  140. extern template class OuterAnalysisManagerProxy<FunctionAnalysisManager, Loop,
  141. LoopStandardAnalysisResults &>;
  142. /// A proxy from a \c FunctionAnalysisManager to a \c Loop.
  143. typedef OuterAnalysisManagerProxy<FunctionAnalysisManager, Loop,
  144. LoopStandardAnalysisResults &>
  145. FunctionAnalysisManagerLoopProxy;
  146. /// Returns the minimum set of Analyses that all loop passes must preserve.
  147. PreservedAnalyses getLoopPassPreservedAnalyses();
  148. }
  149. #endif // LLVM_ANALYSIS_LOOPANALYSISMANAGER_H
  150. #ifdef __GNUC__
  151. #pragma GCC diagnostic pop
  152. #endif