blob: cde9b873795eb674172473f3ae94bdb35e596b8b [file] [log] [blame]
Hans Wennborgf88287f2014-03-19 18:41:38 +00001//===- PassManager.cpp - Infrastructure for managing & running IR passes --===//
Chandler Carruth74015a72013-11-13 01:12:08 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chandler Carruth74015a72013-11-13 01:12:08 +00006//
7//===----------------------------------------------------------------------===//
8
Chandler Carruth74a8a222016-06-17 07:15:29 +00009#include "llvm/IR/PassManager.h"
Chandler Carruth74015a72013-11-13 01:12:08 +000010#include "llvm/ADT/STLExtras.h"
Juergen Ributzka34390c72014-05-16 02:33:15 +000011#include "llvm/IR/LLVMContext.h"
Chandler Carruth74015a72013-11-13 01:12:08 +000012
13using namespace llvm;
Chandler Carrutha13f27c2014-01-11 11:52:05 +000014
Chandler Carruth6b981642016-12-10 06:34:44 +000015// Explicit template instantiations and specialization defininitions for core
16// template typedefs.
Chandler Carruth2a540942016-02-27 10:38:10 +000017namespace llvm {
Chandler Carruth8abdf752016-08-20 04:57:28 +000018template class AllAnalysesOn<Module>;
19template class AllAnalysesOn<Function>;
Chandler Carruthafcec4c2016-02-27 10:45:35 +000020template class PassManager<Module>;
21template class PassManager<Function>;
22template class AnalysisManager<Module>;
23template class AnalysisManager<Function>;
Chandler Carruth2a540942016-02-27 10:38:10 +000024template class InnerAnalysisManagerProxy<FunctionAnalysisManager, Module>;
25template class OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>;
Chandler Carruth6b981642016-12-10 06:34:44 +000026
27template <>
28bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
29 Module &M, const PreservedAnalyses &PA,
30 ModuleAnalysisManager::Invalidator &Inv) {
Chandler Carruthba90ae92016-12-27 08:40:39 +000031 // If literally everything is preserved, we're done.
32 if (PA.areAllPreserved())
33 return false; // This is still a valid proxy.
34
Chandler Carruth6b981642016-12-10 06:34:44 +000035 // 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.
Chandler Carruthba90ae92016-12-27 08:40:39 +000043 auto PAC = PA.getChecker<FunctionAnalysisManagerModuleProxy>();
44 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Module>>()) {
Chandler Carruth6b981642016-12-10 06:34:44 +000045 InnerAM->clear();
46 return true;
47 }
48
Chandler Carruthba90ae92016-12-27 08:40:39 +000049 // 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 }
Chandler Carruth6b981642016-12-10 06:34:44 +000087
88 // Return false to indicate that this result is still a valid proxy.
89 return false;
90}
Chandler Carruth851a2aa2013-11-21 02:11:31 +000091}
Chandler Carruthdab4eae2016-11-23 17:53:26 +000092
Chandler Carruthca68a3e2017-01-15 06:32:49 +000093AnalysisSetKey CFGAnalyses::SetKey;
94
Chandler Carruthba90ae92016-12-27 08:40:39 +000095AnalysisSetKey PreservedAnalyses::AllAnalysesKey;