ScopPass.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //===- ScopPass.cpp - The base class of Passes that operate on Polly IR ---===//
  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. //
  9. // This file contains the definitions of the ScopPass members.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "polly/ScopPass.h"
  13. #include "polly/ScopInfo.h"
  14. #include "llvm/Analysis/BasicAliasAnalysis.h"
  15. #include "llvm/Analysis/GlobalsModRef.h"
  16. #include "llvm/Analysis/LazyBlockFrequencyInfo.h"
  17. #include "llvm/Analysis/LazyBranchProbabilityInfo.h"
  18. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  19. #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
  20. #include "llvm/Analysis/TargetTransformInfo.h"
  21. using namespace llvm;
  22. using namespace polly;
  23. bool ScopPass::runOnRegion(Region *R, RGPassManager &RGM) {
  24. S = nullptr;
  25. if (skipRegion(*R))
  26. return false;
  27. if ((S = getAnalysis<ScopInfoRegionPass>().getScop()))
  28. return runOnScop(*S);
  29. return false;
  30. }
  31. void ScopPass::print(raw_ostream &OS, const Module *M) const {
  32. if (S)
  33. printScop(OS, *S);
  34. }
  35. void ScopPass::getAnalysisUsage(AnalysisUsage &AU) const {
  36. AU.addRequired<ScopInfoRegionPass>();
  37. AU.addPreserved<AAResultsWrapperPass>();
  38. AU.addPreserved<BasicAAWrapperPass>();
  39. AU.addPreserved<LoopInfoWrapperPass>();
  40. AU.addPreserved<DominatorTreeWrapperPass>();
  41. AU.addPreserved<GlobalsAAWrapperPass>();
  42. AU.addPreserved<ScopDetectionWrapperPass>();
  43. AU.addPreserved<ScalarEvolutionWrapperPass>();
  44. AU.addPreserved<SCEVAAWrapperPass>();
  45. AU.addPreserved<OptimizationRemarkEmitterWrapperPass>();
  46. AU.addPreserved<LazyBlockFrequencyInfoPass>();
  47. AU.addPreserved<LazyBranchProbabilityInfoPass>();
  48. AU.addPreserved<RegionInfoPass>();
  49. AU.addPreserved<ScopInfoRegionPass>();
  50. AU.addPreserved<TargetTransformInfoWrapperPass>();
  51. }
  52. namespace polly {
  53. template class OwningInnerAnalysisManagerProxy<ScopAnalysisManager, Function>;
  54. }
  55. namespace llvm {
  56. template class PassManager<Scop, ScopAnalysisManager,
  57. ScopStandardAnalysisResults &, SPMUpdater &>;
  58. template class InnerAnalysisManagerProxy<ScopAnalysisManager, Function>;
  59. template class OuterAnalysisManagerProxy<FunctionAnalysisManager, Scop,
  60. ScopStandardAnalysisResults &>;
  61. template <>
  62. PreservedAnalyses
  63. PassManager<Scop, ScopAnalysisManager, ScopStandardAnalysisResults &,
  64. SPMUpdater &>::run(Scop &S, ScopAnalysisManager &AM,
  65. ScopStandardAnalysisResults &AR, SPMUpdater &U) {
  66. auto PA = PreservedAnalyses::all();
  67. for (auto &Pass : Passes) {
  68. auto PassPA = Pass->run(S, AM, AR, U);
  69. AM.invalidate(S, PassPA);
  70. PA.intersect(std::move(PassPA));
  71. }
  72. // All analyses for 'this' Scop have been invalidated above.
  73. // If ScopPasses affect break other scops they have to propagate this
  74. // information through the updater
  75. PA.preserveSet<AllAnalysesOn<Scop>>();
  76. return PA;
  77. }
  78. bool ScopAnalysisManagerFunctionProxy::Result::invalidate(
  79. Function &F, const PreservedAnalyses &PA,
  80. FunctionAnalysisManager::Invalidator &Inv) {
  81. // First, check whether our ScopInfo is about to be invalidated
  82. auto PAC = PA.getChecker<ScopAnalysisManagerFunctionProxy>();
  83. if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
  84. Inv.invalidate<ScopInfoAnalysis>(F, PA) ||
  85. Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) ||
  86. Inv.invalidate<LoopAnalysis>(F, PA) ||
  87. Inv.invalidate<DominatorTreeAnalysis>(F, PA)) {
  88. // As everything depends on ScopInfo, we must drop all existing results
  89. for (auto &S : *SI)
  90. if (auto *scop = S.second.get())
  91. if (InnerAM)
  92. InnerAM->clear(*scop, scop->getName());
  93. InnerAM = nullptr;
  94. return true; // Invalidate the proxy result as well.
  95. }
  96. bool allPreserved = PA.allAnalysesInSetPreserved<AllAnalysesOn<Scop>>();
  97. // Invalidate all non-preserved analyses
  98. // Even if all analyses were preserved, we still need to run deferred
  99. // invalidation
  100. for (auto &S : *SI) {
  101. Optional<PreservedAnalyses> InnerPA;
  102. auto *scop = S.second.get();
  103. if (!scop)
  104. continue;
  105. if (auto *OuterProxy =
  106. InnerAM->getCachedResult<FunctionAnalysisManagerScopProxy>(*scop)) {
  107. for (const auto &InvPair : OuterProxy->getOuterInvalidations()) {
  108. auto *OuterAnalysisID = InvPair.first;
  109. const auto &InnerAnalysisIDs = InvPair.second;
  110. if (Inv.invalidate(OuterAnalysisID, F, PA)) {
  111. if (!InnerPA)
  112. InnerPA = PA;
  113. for (auto *InnerAnalysisID : InnerAnalysisIDs)
  114. InnerPA->abandon(InnerAnalysisID);
  115. }
  116. }
  117. if (InnerPA) {
  118. InnerAM->invalidate(*scop, *InnerPA);
  119. continue;
  120. }
  121. }
  122. if (!allPreserved)
  123. InnerAM->invalidate(*scop, PA);
  124. }
  125. return false; // This proxy is still valid
  126. }
  127. template <>
  128. ScopAnalysisManagerFunctionProxy::Result
  129. ScopAnalysisManagerFunctionProxy::run(Function &F,
  130. FunctionAnalysisManager &FAM) {
  131. return Result(*InnerAM, FAM.getResult<ScopInfoAnalysis>(F));
  132. }
  133. } // namespace llvm
  134. namespace polly {
  135. template <>
  136. OwningScopAnalysisManagerFunctionProxy::Result
  137. OwningScopAnalysisManagerFunctionProxy::run(Function &F,
  138. FunctionAnalysisManager &FAM) {
  139. return Result(InnerAM, FAM.getResult<ScopInfoAnalysis>(F));
  140. }
  141. } // namespace polly