blob: 47fdfedfdde815836eec739c08e7f3f3354b8150 [file] [log] [blame]
Hans Wennborga2ff1c52014-03-19 18:41:38 +00001//===- PassManager.cpp - Infrastructure for managing & running IR passes --===//
Chandler Carruthf348c972013-11-13 01:12:08 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chandler Carruthbd6882c2016-06-17 07:15:29 +000010#include "llvm/IR/PassManager.h"
Chandler Carruthf348c972013-11-13 01:12:08 +000011#include "llvm/ADT/STLExtras.h"
Juergen Ributzka9bc1b732014-05-16 02:33:15 +000012#include "llvm/IR/LLVMContext.h"
Chandler Carruthf348c972013-11-13 01:12:08 +000013
14using namespace llvm;
Chandler Carruthb56749c2014-01-11 11:52:05 +000015
Chandler Carruth8bf27802016-12-10 06:34:44 +000016// Explicit template instantiations and specialization defininitions for core
17// template typedefs.
Chandler Carruth500af852016-02-27 10:38:10 +000018namespace llvm {
Chandler Carruth65b92672016-08-20 04:57:28 +000019template class AllAnalysesOn<Module>;
20template class AllAnalysesOn<Function>;
Chandler Carrutha36b27d2016-02-27 10:45:35 +000021template class PassManager<Module>;
22template class PassManager<Function>;
23template class AnalysisManager<Module>;
24template class AnalysisManager<Function>;
Chandler Carruth500af852016-02-27 10:38:10 +000025template class InnerAnalysisManagerProxy<FunctionAnalysisManager, Module>;
26template class OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>;
Chandler Carruth8bf27802016-12-10 06:34:44 +000027
28template <>
29bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
30 Module &M, const PreservedAnalyses &PA,
31 ModuleAnalysisManager::Invalidator &Inv) {
Chandler Carruth0fc44672016-12-27 08:40:39 +000032 // If literally everything is preserved, we're done.
33 if (PA.areAllPreserved())
34 return false; // This is still a valid proxy.
35
Chandler Carruth8bf27802016-12-10 06:34:44 +000036 // If this proxy isn't marked as preserved, then even if the result remains
37 // valid, the key itself may no longer be valid, so we clear everything.
38 //
39 // Note that in order to preserve this proxy, a module pass must ensure that
40 // the FAM has been completely updated to handle the deletion of functions.
41 // Specifically, any FAM-cached results for those functions need to have been
42 // forcibly cleared. When preserved, this proxy will only invalidate results
43 // cached on functions *still in the module* at the end of the module pass.
Chandler Carruth0fc44672016-12-27 08:40:39 +000044 auto PAC = PA.getChecker<FunctionAnalysisManagerModuleProxy>();
45 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Module>>()) {
Chandler Carruth8bf27802016-12-10 06:34:44 +000046 InnerAM->clear();
47 return true;
48 }
49
Chandler Carruth0fc44672016-12-27 08:40:39 +000050 // Directly check if the relevant set is preserved.
51 bool AreFunctionAnalysesPreserved =
52 PA.allAnalysesInSetPreserved<AllAnalysesOn<Function>>();
53
54 // Now walk all the functions to see if any inner analysis invalidation is
55 // necessary.
56 for (Function &F : M) {
57 Optional<PreservedAnalyses> FunctionPA;
58
59 // Check to see whether the preserved set needs to be pruned based on
60 // module-level analysis invalidation that triggers deferred invalidation
61 // registered with the outer analysis manager proxy for this function.
62 if (auto *OuterProxy =
63 InnerAM->getCachedResult<ModuleAnalysisManagerFunctionProxy>(F))
64 for (const auto &OuterInvalidationPair :
65 OuterProxy->getOuterInvalidations()) {
66 AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
67 const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
68 if (Inv.invalidate(OuterAnalysisID, M, PA)) {
69 if (!FunctionPA)
70 FunctionPA = PA;
71 for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
72 FunctionPA->abandon(InnerAnalysisID);
73 }
74 }
75
76 // Check if we needed a custom PA set, and if so we'll need to run the
77 // inner invalidation.
78 if (FunctionPA) {
79 InnerAM->invalidate(F, *FunctionPA);
80 continue;
81 }
82
83 // Otherwise we only need to do invalidation if the original PA set didn't
84 // preserve all function analyses.
85 if (!AreFunctionAnalysesPreserved)
86 InnerAM->invalidate(F, PA);
87 }
Chandler Carruth8bf27802016-12-10 06:34:44 +000088
89 // Return false to indicate that this result is still a valid proxy.
90 return false;
91}
Chandler Carruth7fac06c2013-11-21 02:11:31 +000092}
Chandler Carruth33d56812016-11-23 17:53:26 +000093
Chandler Carruth10dd00c2017-01-15 06:32:49 +000094AnalysisSetKey CFGAnalyses::SetKey;
95
Chandler Carruth0fc44672016-12-27 08:40:39 +000096AnalysisSetKey PreservedAnalyses::AllAnalysesKey;