PassManagerImpl.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- PassManagerImpl.h - Pass management infrastructure -------*- 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. /// Provides implementations for PassManager and AnalysisManager template
  15. /// methods. These classes should be explicitly instantiated for any IR unit,
  16. /// and files doing the explicit instantiation should include this header.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_IR_PASSMANAGERIMPL_H
  20. #define LLVM_IR_PASSMANAGERIMPL_H
  21. #include "llvm/IR/PassManager.h"
  22. namespace llvm {
  23. template <typename IRUnitT, typename... ExtraArgTs>
  24. inline AnalysisManager<IRUnitT, ExtraArgTs...>::AnalysisManager() = default;
  25. template <typename IRUnitT, typename... ExtraArgTs>
  26. inline AnalysisManager<IRUnitT, ExtraArgTs...>::AnalysisManager(
  27. AnalysisManager &&) = default;
  28. template <typename IRUnitT, typename... ExtraArgTs>
  29. inline AnalysisManager<IRUnitT, ExtraArgTs...> &
  30. AnalysisManager<IRUnitT, ExtraArgTs...>::operator=(AnalysisManager &&) =
  31. default;
  32. template <typename IRUnitT, typename... ExtraArgTs>
  33. inline void
  34. AnalysisManager<IRUnitT, ExtraArgTs...>::clear(IRUnitT &IR,
  35. llvm::StringRef Name) {
  36. if (auto *PI = getCachedResult<PassInstrumentationAnalysis>(IR))
  37. PI->runAnalysesCleared(Name);
  38. auto ResultsListI = AnalysisResultLists.find(&IR);
  39. if (ResultsListI == AnalysisResultLists.end())
  40. return;
  41. // Delete the map entries that point into the results list.
  42. for (auto &IDAndResult : ResultsListI->second)
  43. AnalysisResults.erase({IDAndResult.first, &IR});
  44. // And actually destroy and erase the results associated with this IR.
  45. AnalysisResultLists.erase(ResultsListI);
  46. }
  47. template <typename IRUnitT, typename... ExtraArgTs>
  48. inline typename AnalysisManager<IRUnitT, ExtraArgTs...>::ResultConceptT &
  49. AnalysisManager<IRUnitT, ExtraArgTs...>::getResultImpl(
  50. AnalysisKey *ID, IRUnitT &IR, ExtraArgTs... ExtraArgs) {
  51. typename AnalysisResultMapT::iterator RI;
  52. bool Inserted;
  53. std::tie(RI, Inserted) = AnalysisResults.insert(std::make_pair(
  54. std::make_pair(ID, &IR), typename AnalysisResultListT::iterator()));
  55. // If we don't have a cached result for this function, look up the pass and
  56. // run it to produce a result, which we then add to the cache.
  57. if (Inserted) {
  58. auto &P = this->lookUpPass(ID);
  59. PassInstrumentation PI;
  60. if (ID != PassInstrumentationAnalysis::ID()) {
  61. PI = getResult<PassInstrumentationAnalysis>(IR, ExtraArgs...);
  62. PI.runBeforeAnalysis(P, IR);
  63. }
  64. AnalysisResultListT &ResultList = AnalysisResultLists[&IR];
  65. ResultList.emplace_back(ID, P.run(IR, *this, ExtraArgs...));
  66. PI.runAfterAnalysis(P, IR);
  67. // P.run may have inserted elements into AnalysisResults and invalidated
  68. // RI.
  69. RI = AnalysisResults.find({ID, &IR});
  70. assert(RI != AnalysisResults.end() && "we just inserted it!");
  71. RI->second = std::prev(ResultList.end());
  72. }
  73. return *RI->second->second;
  74. }
  75. template <typename IRUnitT, typename... ExtraArgTs>
  76. inline void AnalysisManager<IRUnitT, ExtraArgTs...>::invalidate(
  77. IRUnitT &IR, const PreservedAnalyses &PA) {
  78. // We're done if all analyses on this IR unit are preserved.
  79. if (PA.allAnalysesInSetPreserved<AllAnalysesOn<IRUnitT>>())
  80. return;
  81. // Track whether each analysis's result is invalidated in
  82. // IsResultInvalidated.
  83. SmallDenseMap<AnalysisKey *, bool, 8> IsResultInvalidated;
  84. Invalidator Inv(IsResultInvalidated, AnalysisResults);
  85. AnalysisResultListT &ResultsList = AnalysisResultLists[&IR];
  86. for (auto &AnalysisResultPair : ResultsList) {
  87. // This is basically the same thing as Invalidator::invalidate, but we
  88. // can't call it here because we're operating on the type-erased result.
  89. // Moreover if we instead called invalidate() directly, it would do an
  90. // unnecessary look up in ResultsList.
  91. AnalysisKey *ID = AnalysisResultPair.first;
  92. auto &Result = *AnalysisResultPair.second;
  93. auto IMapI = IsResultInvalidated.find(ID);
  94. if (IMapI != IsResultInvalidated.end())
  95. // This result was already handled via the Invalidator.
  96. continue;
  97. // Try to invalidate the result, giving it the Invalidator so it can
  98. // recursively query for any dependencies it has and record the result.
  99. // Note that we cannot reuse 'IMapI' here or pre-insert the ID, as
  100. // Result.invalidate may insert things into the map, invalidating our
  101. // iterator.
  102. bool Inserted =
  103. IsResultInvalidated.insert({ID, Result.invalidate(IR, PA, Inv)}).second;
  104. (void)Inserted;
  105. assert(Inserted && "Should never have already inserted this ID, likely "
  106. "indicates a cycle!");
  107. }
  108. // Now erase the results that were marked above as invalidated.
  109. if (!IsResultInvalidated.empty()) {
  110. for (auto I = ResultsList.begin(), E = ResultsList.end(); I != E;) {
  111. AnalysisKey *ID = I->first;
  112. if (!IsResultInvalidated.lookup(ID)) {
  113. ++I;
  114. continue;
  115. }
  116. if (auto *PI = getCachedResult<PassInstrumentationAnalysis>(IR))
  117. PI->runAnalysisInvalidated(this->lookUpPass(ID), IR);
  118. I = ResultsList.erase(I);
  119. AnalysisResults.erase({ID, &IR});
  120. }
  121. }
  122. if (ResultsList.empty())
  123. AnalysisResultLists.erase(&IR);
  124. }
  125. } // end namespace llvm
  126. #endif // LLVM_IR_PASSMANAGERIMPL_H
  127. #ifdef __GNUC__
  128. #pragma GCC diagnostic pop
  129. #endif