LoopAnalysisManager.h 6.0 KB

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