PassManager.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/STLExtras.h"
  10. #include "llvm/IR/LLVMContext.h"
  11. using namespace llvm;
  12. // Explicit template instantiations and specialization defininitions for core
  13. // template typedefs.
  14. namespace llvm {
  15. template class AllAnalysesOn<Module>;
  16. template class AllAnalysesOn<Function>;
  17. template class PassManager<Module>;
  18. template class PassManager<Function>;
  19. template class AnalysisManager<Module>;
  20. template class AnalysisManager<Function>;
  21. template class InnerAnalysisManagerProxy<FunctionAnalysisManager, Module>;
  22. template class OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>;
  23. template <>
  24. bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
  25. Module &M, const PreservedAnalyses &PA,
  26. ModuleAnalysisManager::Invalidator &Inv) {
  27. // If literally everything is preserved, we're done.
  28. if (PA.areAllPreserved())
  29. return false; // This is still a valid proxy.
  30. // If this proxy isn't marked as preserved, then even if the result remains
  31. // valid, the key itself may no longer be valid, so we clear everything.
  32. //
  33. // Note that in order to preserve this proxy, a module pass must ensure that
  34. // the FAM has been completely updated to handle the deletion of functions.
  35. // Specifically, any FAM-cached results for those functions need to have been
  36. // forcibly cleared. When preserved, this proxy will only invalidate results
  37. // cached on functions *still in the module* at the end of the module pass.
  38. auto PAC = PA.getChecker<FunctionAnalysisManagerModuleProxy>();
  39. if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Module>>()) {
  40. InnerAM->clear();
  41. return true;
  42. }
  43. // Directly check if the relevant set is preserved.
  44. bool AreFunctionAnalysesPreserved =
  45. PA.allAnalysesInSetPreserved<AllAnalysesOn<Function>>();
  46. // Now walk all the functions to see if any inner analysis invalidation is
  47. // necessary.
  48. for (Function &F : M) {
  49. Optional<PreservedAnalyses> FunctionPA;
  50. // Check to see whether the preserved set needs to be pruned based on
  51. // module-level analysis invalidation that triggers deferred invalidation
  52. // registered with the outer analysis manager proxy for this function.
  53. if (auto *OuterProxy =
  54. InnerAM->getCachedResult<ModuleAnalysisManagerFunctionProxy>(F))
  55. for (const auto &OuterInvalidationPair :
  56. OuterProxy->getOuterInvalidations()) {
  57. AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
  58. const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
  59. if (Inv.invalidate(OuterAnalysisID, M, PA)) {
  60. if (!FunctionPA)
  61. FunctionPA = PA;
  62. for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
  63. FunctionPA->abandon(InnerAnalysisID);
  64. }
  65. }
  66. // Check if we needed a custom PA set, and if so we'll need to run the
  67. // inner invalidation.
  68. if (FunctionPA) {
  69. InnerAM->invalidate(F, *FunctionPA);
  70. continue;
  71. }
  72. // Otherwise we only need to do invalidation if the original PA set didn't
  73. // preserve all function analyses.
  74. if (!AreFunctionAnalysesPreserved)
  75. InnerAM->invalidate(F, PA);
  76. }
  77. // Return false to indicate that this result is still a valid proxy.
  78. return false;
  79. }
  80. }
  81. AnalysisSetKey CFGAnalyses::SetKey;
  82. AnalysisSetKey PreservedAnalyses::AllAnalysesKey;