PassManager.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //===- PassManager.cpp - Infrastructure for managing & running IR passes --===//
  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/IR/PassManager.h"
  9. #include "llvm/ADT/DenseMapInfo.h"
  10. #include "llvm/ADT/Optional.h"
  11. #include "llvm/IR/PassManagerImpl.h"
  12. using namespace llvm;
  13. namespace llvm {
  14. // Explicit template instantiations and specialization defininitions for core
  15. // template typedefs.
  16. template class AllAnalysesOn<Module>;
  17. template class AllAnalysesOn<Function>;
  18. template class PassManager<Module>;
  19. template class PassManager<Function>;
  20. template class AnalysisManager<Module>;
  21. template class AnalysisManager<Function>;
  22. template class InnerAnalysisManagerProxy<FunctionAnalysisManager, Module>;
  23. template class OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>;
  24. template <>
  25. bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
  26. Module &M, const PreservedAnalyses &PA,
  27. ModuleAnalysisManager::Invalidator &Inv) {
  28. // If literally everything is preserved, we're done.
  29. if (PA.areAllPreserved())
  30. return false; // This is still a valid proxy.
  31. // If this proxy isn't marked as preserved, then even if the result remains
  32. // valid, the key itself may no longer be valid, so we clear everything.
  33. //
  34. // Note that in order to preserve this proxy, a module pass must ensure that
  35. // the FAM has been completely updated to handle the deletion of functions.
  36. // Specifically, any FAM-cached results for those functions need to have been
  37. // forcibly cleared. When preserved, this proxy will only invalidate results
  38. // cached on functions *still in the module* at the end of the module pass.
  39. auto PAC = PA.getChecker<FunctionAnalysisManagerModuleProxy>();
  40. if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Module>>()) {
  41. InnerAM->clear();
  42. return true;
  43. }
  44. // Directly check if the relevant set is preserved.
  45. bool AreFunctionAnalysesPreserved =
  46. PA.allAnalysesInSetPreserved<AllAnalysesOn<Function>>();
  47. // Now walk all the functions to see if any inner analysis invalidation is
  48. // necessary.
  49. for (Function &F : M) {
  50. Optional<PreservedAnalyses> FunctionPA;
  51. // Check to see whether the preserved set needs to be pruned based on
  52. // module-level analysis invalidation that triggers deferred invalidation
  53. // registered with the outer analysis manager proxy for this function.
  54. if (auto *OuterProxy =
  55. InnerAM->getCachedResult<ModuleAnalysisManagerFunctionProxy>(F))
  56. for (const auto &OuterInvalidationPair :
  57. OuterProxy->getOuterInvalidations()) {
  58. AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
  59. const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
  60. if (Inv.invalidate(OuterAnalysisID, M, PA)) {
  61. if (!FunctionPA)
  62. FunctionPA = PA;
  63. for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
  64. FunctionPA->abandon(InnerAnalysisID);
  65. }
  66. }
  67. // Check if we needed a custom PA set, and if so we'll need to run the
  68. // inner invalidation.
  69. if (FunctionPA) {
  70. InnerAM->invalidate(F, *FunctionPA);
  71. continue;
  72. }
  73. // Otherwise we only need to do invalidation if the original PA set didn't
  74. // preserve all function analyses.
  75. if (!AreFunctionAnalysesPreserved)
  76. InnerAM->invalidate(F, PA);
  77. }
  78. // Return false to indicate that this result is still a valid proxy.
  79. return false;
  80. }
  81. } // namespace llvm
  82. void ModuleToFunctionPassAdaptor::printPipeline(
  83. raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
  84. OS << "function";
  85. if (EagerlyInvalidate)
  86. OS << "<eager-inv>";
  87. OS << "(";
  88. Pass->printPipeline(OS, MapClassName2PassName);
  89. OS << ")";
  90. }
  91. PreservedAnalyses ModuleToFunctionPassAdaptor::run(Module &M,
  92. ModuleAnalysisManager &AM) {
  93. FunctionAnalysisManager &FAM =
  94. AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
  95. // Request PassInstrumentation from analysis manager, will use it to run
  96. // instrumenting callbacks for the passes later.
  97. PassInstrumentation PI = AM.getResult<PassInstrumentationAnalysis>(M);
  98. PreservedAnalyses PA = PreservedAnalyses::all();
  99. for (Function &F : M) {
  100. if (F.isDeclaration())
  101. continue;
  102. // Check the PassInstrumentation's BeforePass callbacks before running the
  103. // pass, skip its execution completely if asked to (callback returns
  104. // false).
  105. if (!PI.runBeforePass<Function>(*Pass, F))
  106. continue;
  107. PreservedAnalyses PassPA;
  108. {
  109. TimeTraceScope TimeScope(Pass->name(), F.getName());
  110. PassPA = Pass->run(F, FAM);
  111. }
  112. PI.runAfterPass(*Pass, F, PassPA);
  113. // We know that the function pass couldn't have invalidated any other
  114. // function's analyses (that's the contract of a function pass), so
  115. // directly handle the function analysis manager's invalidation here.
  116. FAM.invalidate(F, EagerlyInvalidate ? PreservedAnalyses::none() : PassPA);
  117. // Then intersect the preserved set so that invalidation of module
  118. // analyses will eventually occur when the module pass completes.
  119. PA.intersect(std::move(PassPA));
  120. }
  121. // The FunctionAnalysisManagerModuleProxy is preserved because (we assume)
  122. // the function passes we ran didn't add or remove any functions.
  123. //
  124. // We also preserve all analyses on Functions, because we did all the
  125. // invalidation we needed to do above.
  126. PA.preserveSet<AllAnalysesOn<Function>>();
  127. PA.preserve<FunctionAnalysisManagerModuleProxy>();
  128. return PA;
  129. }
  130. AnalysisSetKey CFGAnalyses::SetKey;
  131. AnalysisSetKey PreservedAnalyses::AllAnalysesKey;