Hans Wennborg | f88287f | 2014-03-19 18:41:38 +0000 | [diff] [blame] | 1 | //===- PassManager.cpp - Infrastructure for managing & running IR passes --===// |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Chandler Carruth | 74a8a22 | 2016-06-17 07:15:29 +0000 | [diff] [blame] | 9 | #include "llvm/IR/PassManager.h" |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/STLExtras.h" |
Juergen Ributzka | 34390c7 | 2014-05-16 02:33:15 +0000 | [diff] [blame] | 11 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 12 | |
| 13 | using namespace llvm; |
Chandler Carruth | a13f27c | 2014-01-11 11:52:05 +0000 | [diff] [blame] | 14 | |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 15 | // Explicit template instantiations and specialization defininitions for core |
| 16 | // template typedefs. |
Chandler Carruth | 2a54094 | 2016-02-27 10:38:10 +0000 | [diff] [blame] | 17 | namespace llvm { |
Chandler Carruth | 8abdf75 | 2016-08-20 04:57:28 +0000 | [diff] [blame] | 18 | template class AllAnalysesOn<Module>; |
| 19 | template class AllAnalysesOn<Function>; |
Chandler Carruth | afcec4c | 2016-02-27 10:45:35 +0000 | [diff] [blame] | 20 | template class PassManager<Module>; |
| 21 | template class PassManager<Function>; |
| 22 | template class AnalysisManager<Module>; |
| 23 | template class AnalysisManager<Function>; |
Chandler Carruth | 2a54094 | 2016-02-27 10:38:10 +0000 | [diff] [blame] | 24 | template class InnerAnalysisManagerProxy<FunctionAnalysisManager, Module>; |
| 25 | template class OuterAnalysisManagerProxy<ModuleAnalysisManager, Function>; |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 26 | |
| 27 | template <> |
| 28 | bool FunctionAnalysisManagerModuleProxy::Result::invalidate( |
| 29 | Module &M, const PreservedAnalyses &PA, |
| 30 | ModuleAnalysisManager::Invalidator &Inv) { |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 31 | // If literally everything is preserved, we're done. |
| 32 | if (PA.areAllPreserved()) |
| 33 | return false; // This is still a valid proxy. |
| 34 | |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 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. |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 43 | auto PAC = PA.getChecker<FunctionAnalysisManagerModuleProxy>(); |
| 44 | if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Module>>()) { |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 45 | InnerAM->clear(); |
| 46 | return true; |
| 47 | } |
| 48 | |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 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 | } |
Chandler Carruth | 6b98164 | 2016-12-10 06:34:44 +0000 | [diff] [blame] | 87 | |
| 88 | // Return false to indicate that this result is still a valid proxy. |
| 89 | return false; |
| 90 | } |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 91 | } |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 92 | |
Chandler Carruth | ca68a3e | 2017-01-15 06:32:49 +0000 | [diff] [blame] | 93 | AnalysisSetKey CFGAnalyses::SetKey; |
| 94 | |
Chandler Carruth | ba90ae9 | 2016-12-27 08:40:39 +0000 | [diff] [blame] | 95 | AnalysisSetKey PreservedAnalyses::AllAnalysesKey; |