LoopAnalysisManager.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //===- LoopAnalysisManager.cpp - Loop analysis management -----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/Analysis/LoopAnalysisManager.h"
  9. #include "llvm/Analysis/AssumptionCache.h"
  10. #include "llvm/Analysis/LoopInfo.h"
  11. #include "llvm/Analysis/MemorySSA.h"
  12. #include "llvm/Analysis/ScalarEvolution.h"
  13. #include "llvm/IR/Dominators.h"
  14. #include "llvm/IR/PassManagerImpl.h"
  15. #include <optional>
  16. using namespace llvm;
  17. namespace llvm {
  18. // Explicit template instantiations and specialization definitions for core
  19. // template typedefs.
  20. template class AllAnalysesOn<Loop>;
  21. template class AnalysisManager<Loop, LoopStandardAnalysisResults &>;
  22. template class InnerAnalysisManagerProxy<LoopAnalysisManager, Function>;
  23. template class OuterAnalysisManagerProxy<FunctionAnalysisManager, Loop,
  24. LoopStandardAnalysisResults &>;
  25. bool LoopAnalysisManagerFunctionProxy::Result::invalidate(
  26. Function &F, const PreservedAnalyses &PA,
  27. FunctionAnalysisManager::Invalidator &Inv) {
  28. // First compute the sequence of IR units covered by this proxy. We will want
  29. // to visit this in postorder, but because this is a tree structure we can do
  30. // this by building a preorder sequence and walking it backwards. We also
  31. // want siblings in forward program order to match the LoopPassManager so we
  32. // get the preorder with siblings reversed.
  33. SmallVector<Loop *, 4> PreOrderLoops = LI->getLoopsInReverseSiblingPreorder();
  34. // If this proxy or the loop info is going to be invalidated, we also need
  35. // to clear all the keys coming from that analysis. We also completely blow
  36. // away the loop analyses if any of the standard analyses provided by the
  37. // loop pass manager go away so that loop analyses can freely use these
  38. // without worrying about declaring dependencies on them etc.
  39. // FIXME: It isn't clear if this is the right tradeoff. We could instead make
  40. // loop analyses declare any dependencies on these and use the more general
  41. // invalidation logic below to act on that.
  42. auto PAC = PA.getChecker<LoopAnalysisManagerFunctionProxy>();
  43. bool invalidateMemorySSAAnalysis = false;
  44. if (MSSAUsed)
  45. invalidateMemorySSAAnalysis = Inv.invalidate<MemorySSAAnalysis>(F, PA);
  46. if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
  47. Inv.invalidate<AAManager>(F, PA) ||
  48. Inv.invalidate<AssumptionAnalysis>(F, PA) ||
  49. Inv.invalidate<DominatorTreeAnalysis>(F, PA) ||
  50. Inv.invalidate<LoopAnalysis>(F, PA) ||
  51. Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) ||
  52. invalidateMemorySSAAnalysis) {
  53. // Note that the LoopInfo may be stale at this point, however the loop
  54. // objects themselves remain the only viable keys that could be in the
  55. // analysis manager's cache. So we just walk the keys and forcibly clear
  56. // those results. Note that the order doesn't matter here as this will just
  57. // directly destroy the results without calling methods on them.
  58. for (Loop *L : PreOrderLoops) {
  59. // NB! `L` may not be in a good enough state to run Loop::getName.
  60. InnerAM->clear(*L, "<possibly invalidated loop>");
  61. }
  62. // We also need to null out the inner AM so that when the object gets
  63. // destroyed as invalid we don't try to clear the inner AM again. At that
  64. // point we won't be able to reliably walk the loops for this function and
  65. // only clear results associated with those loops the way we do here.
  66. // FIXME: Making InnerAM null at this point isn't very nice. Most analyses
  67. // try to remain valid during invalidation. Maybe we should add an
  68. // `IsClean` flag?
  69. InnerAM = nullptr;
  70. // Now return true to indicate this *is* invalid and a fresh proxy result
  71. // needs to be built. This is especially important given the null InnerAM.
  72. return true;
  73. }
  74. // Directly check if the relevant set is preserved so we can short circuit
  75. // invalidating loops.
  76. bool AreLoopAnalysesPreserved =
  77. PA.allAnalysesInSetPreserved<AllAnalysesOn<Loop>>();
  78. // Since we have a valid LoopInfo we can actually leave the cached results in
  79. // the analysis manager associated with the Loop keys, but we need to
  80. // propagate any necessary invalidation logic into them. We'd like to
  81. // invalidate things in roughly the same order as they were put into the
  82. // cache and so we walk the preorder list in reverse to form a valid
  83. // postorder.
  84. for (Loop *L : reverse(PreOrderLoops)) {
  85. std::optional<PreservedAnalyses> InnerPA;
  86. // Check to see whether the preserved set needs to be adjusted based on
  87. // function-level analysis invalidation triggering deferred invalidation
  88. // for this loop.
  89. if (auto *OuterProxy =
  90. InnerAM->getCachedResult<FunctionAnalysisManagerLoopProxy>(*L))
  91. for (const auto &OuterInvalidationPair :
  92. OuterProxy->getOuterInvalidations()) {
  93. AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
  94. const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
  95. if (Inv.invalidate(OuterAnalysisID, F, PA)) {
  96. if (!InnerPA)
  97. InnerPA = PA;
  98. for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
  99. InnerPA->abandon(InnerAnalysisID);
  100. }
  101. }
  102. // Check if we needed a custom PA set. If so we'll need to run the inner
  103. // invalidation.
  104. if (InnerPA) {
  105. InnerAM->invalidate(*L, *InnerPA);
  106. continue;
  107. }
  108. // Otherwise we only need to do invalidation if the original PA set didn't
  109. // preserve all Loop analyses.
  110. if (!AreLoopAnalysesPreserved)
  111. InnerAM->invalidate(*L, PA);
  112. }
  113. // Return false to indicate that this result is still a valid proxy.
  114. return false;
  115. }
  116. template <>
  117. LoopAnalysisManagerFunctionProxy::Result
  118. LoopAnalysisManagerFunctionProxy::run(Function &F,
  119. FunctionAnalysisManager &AM) {
  120. return Result(*InnerAM, AM.getResult<LoopAnalysis>(F));
  121. }
  122. }
  123. PreservedAnalyses llvm::getLoopPassPreservedAnalyses() {
  124. PreservedAnalyses PA;
  125. PA.preserve<DominatorTreeAnalysis>();
  126. PA.preserve<LoopAnalysis>();
  127. PA.preserve<LoopAnalysisManagerFunctionProxy>();
  128. PA.preserve<ScalarEvolutionAnalysis>();
  129. return PA;
  130. }