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