LoopAnalysisManager.cpp 6.4 KB

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