Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 1 | //===- PassManager.h - Infrastructure for managing & running IR passes ----===// |
| 2 | // |
| 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 | |
| 10 | #include "llvm/IR/PassManager.h" |
| 11 | #include "llvm/ADT/STLExtras.h" |
| 12 | |
| 13 | using namespace llvm; |
| 14 | |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 15 | PreservedAnalyses ModulePassManager::run(Module *M, ModuleAnalysisManager *AM) { |
Chandler Carruth | c0bfa8c | 2013-11-20 11:31:50 +0000 | [diff] [blame] | 16 | PreservedAnalyses PA = PreservedAnalyses::all(); |
| 17 | for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) { |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 18 | PreservedAnalyses PassPA = Passes[Idx]->run(M, AM); |
Chandler Carruth | c0bfa8c | 2013-11-20 11:31:50 +0000 | [diff] [blame] | 19 | if (AM) |
| 20 | AM->invalidate(M, PassPA); |
| 21 | PA.intersect(llvm_move(PassPA)); |
| 22 | } |
| 23 | return PA; |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame] | 24 | } |
| 25 | |
Chandler Carruth | c0bfa8c | 2013-11-20 11:31:50 +0000 | [diff] [blame] | 26 | void ModuleAnalysisManager::invalidate(Module *M, const PreservedAnalyses &PA) { |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame] | 27 | // FIXME: This is a total hack based on the fact that erasure doesn't |
| 28 | // invalidate iteration for DenseMap. |
| 29 | for (ModuleAnalysisResultMapT::iterator I = ModuleAnalysisResults.begin(), |
| 30 | E = ModuleAnalysisResults.end(); |
| 31 | I != E; ++I) |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 32 | if (I->second->invalidate(M, PA)) |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame] | 33 | ModuleAnalysisResults.erase(I); |
| 34 | } |
| 35 | |
Chandler Carruth | 5bf5e31 | 2013-11-22 11:34:43 +0000 | [diff] [blame] | 36 | const detail::AnalysisResultConcept<Module *> & |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame] | 37 | ModuleAnalysisManager::getResultImpl(void *PassID, Module *M) { |
| 38 | ModuleAnalysisResultMapT::iterator RI; |
| 39 | bool Inserted; |
| 40 | llvm::tie(RI, Inserted) = ModuleAnalysisResults.insert(std::make_pair( |
Chandler Carruth | 5bf5e31 | 2013-11-22 11:34:43 +0000 | [diff] [blame] | 41 | PassID, polymorphic_ptr<detail::AnalysisResultConcept<Module *> >())); |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame] | 42 | |
| 43 | if (Inserted) { |
| 44 | // We don't have a cached result for this result. Look up the pass and run |
| 45 | // it to produce a result, which we then add to the cache. |
| 46 | ModuleAnalysisPassMapT::const_iterator PI = |
| 47 | ModuleAnalysisPasses.find(PassID); |
| 48 | assert(PI != ModuleAnalysisPasses.end() && |
| 49 | "Analysis passes must be registered prior to being queried!"); |
Chandler Carruth | f2edc07 | 2013-11-22 12:11:02 +0000 | [diff] [blame] | 50 | RI->second = PI->second->run(M, this); |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | return *RI->second; |
| 54 | } |
| 55 | |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 56 | const detail::AnalysisResultConcept<Module *> * |
| 57 | ModuleAnalysisManager::getCachedResultImpl(void *PassID, Module *M) const { |
| 58 | ModuleAnalysisResultMapT::const_iterator RI = ModuleAnalysisResults.find(PassID); |
| 59 | return RI == ModuleAnalysisResults.end() ? 0 : &*RI->second; |
| 60 | } |
| 61 | |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame] | 62 | void ModuleAnalysisManager::invalidateImpl(void *PassID, Module *M) { |
| 63 | ModuleAnalysisResults.erase(PassID); |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 66 | PreservedAnalyses FunctionPassManager::run(Function *F, FunctionAnalysisManager *AM) { |
Chandler Carruth | c0bfa8c | 2013-11-20 11:31:50 +0000 | [diff] [blame] | 67 | PreservedAnalyses PA = PreservedAnalyses::all(); |
| 68 | for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) { |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 69 | PreservedAnalyses PassPA = Passes[Idx]->run(F, AM); |
Chandler Carruth | c0bfa8c | 2013-11-20 11:31:50 +0000 | [diff] [blame] | 70 | if (AM) |
| 71 | AM->invalidate(F, PassPA); |
| 72 | PA.intersect(llvm_move(PassPA)); |
| 73 | } |
| 74 | return PA; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Chandler Carruth | c0bfa8c | 2013-11-20 11:31:50 +0000 | [diff] [blame] | 77 | void FunctionAnalysisManager::invalidate(Function *F, const PreservedAnalyses &PA) { |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame] | 78 | // Clear all the invalidated results associated specifically with this |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 79 | // function. |
| 80 | SmallVector<void *, 8> InvalidatedPassIDs; |
| 81 | FunctionAnalysisResultListT &ResultsList = FunctionAnalysisResultLists[F]; |
| 82 | for (FunctionAnalysisResultListT::iterator I = ResultsList.begin(), |
| 83 | E = ResultsList.end(); |
Chandler Carruth | 8c60bc9 | 2013-11-15 21:56:44 +0000 | [diff] [blame] | 84 | I != E;) |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 85 | if (I->second->invalidate(F, PA)) { |
Chandler Carruth | 8c60bc9 | 2013-11-15 21:56:44 +0000 | [diff] [blame] | 86 | InvalidatedPassIDs.push_back(I->first); |
| 87 | I = ResultsList.erase(I); |
| 88 | } else { |
| 89 | ++I; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 90 | } |
| 91 | while (!InvalidatedPassIDs.empty()) |
| 92 | FunctionAnalysisResults.erase( |
| 93 | std::make_pair(InvalidatedPassIDs.pop_back_val(), F)); |
| 94 | } |
| 95 | |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 96 | bool FunctionAnalysisManager::empty() const { |
| 97 | assert(FunctionAnalysisResults.empty() == |
| 98 | FunctionAnalysisResultLists.empty() && |
| 99 | "The storage and index of analysis results disagree on how many there " |
| 100 | "are!"); |
| 101 | return FunctionAnalysisResults.empty(); |
| 102 | } |
| 103 | |
| 104 | void FunctionAnalysisManager::clear() { |
| 105 | FunctionAnalysisResults.clear(); |
| 106 | FunctionAnalysisResultLists.clear(); |
| 107 | } |
| 108 | |
Chandler Carruth | 5bf5e31 | 2013-11-22 11:34:43 +0000 | [diff] [blame] | 109 | const detail::AnalysisResultConcept<Function *> & |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame] | 110 | FunctionAnalysisManager::getResultImpl(void *PassID, Function *F) { |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 111 | FunctionAnalysisResultMapT::iterator RI; |
| 112 | bool Inserted; |
| 113 | llvm::tie(RI, Inserted) = FunctionAnalysisResults.insert(std::make_pair( |
| 114 | std::make_pair(PassID, F), FunctionAnalysisResultListT::iterator())); |
| 115 | |
| 116 | if (Inserted) { |
| 117 | // We don't have a cached result for this result. Look up the pass and run |
| 118 | // it to produce a result, which we then add to the cache. |
| 119 | FunctionAnalysisPassMapT::const_iterator PI = |
| 120 | FunctionAnalysisPasses.find(PassID); |
| 121 | assert(PI != FunctionAnalysisPasses.end() && |
| 122 | "Analysis passes must be registered prior to being queried!"); |
| 123 | FunctionAnalysisResultListT &ResultList = FunctionAnalysisResultLists[F]; |
Chandler Carruth | f2edc07 | 2013-11-22 12:11:02 +0000 | [diff] [blame] | 124 | ResultList.push_back(std::make_pair(PassID, PI->second->run(F, this))); |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 125 | RI->second = llvm::prior(ResultList.end()); |
| 126 | } |
| 127 | |
| 128 | return *RI->second->second; |
| 129 | } |
| 130 | |
Chandler Carruth | de9afd8 | 2013-11-23 00:38:42 +0000 | [diff] [blame] | 131 | const detail::AnalysisResultConcept<Function *> * |
| 132 | FunctionAnalysisManager::getCachedResultImpl(void *PassID, Function *F) const { |
| 133 | FunctionAnalysisResultMapT::const_iterator RI = |
| 134 | FunctionAnalysisResults.find(std::make_pair(PassID, F)); |
| 135 | return RI == FunctionAnalysisResults.end() ? 0 : &*RI->second->second; |
| 136 | } |
| 137 | |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame] | 138 | void FunctionAnalysisManager::invalidateImpl(void *PassID, Function *F) { |
Chandler Carruth | 1205a6c | 2013-11-15 21:44:35 +0000 | [diff] [blame] | 139 | FunctionAnalysisResultMapT::iterator RI = |
| 140 | FunctionAnalysisResults.find(std::make_pair(PassID, F)); |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 141 | if (RI == FunctionAnalysisResults.end()) |
| 142 | return; |
| 143 | |
| 144 | FunctionAnalysisResultLists[F].erase(RI->second); |
| 145 | } |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 146 | |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 147 | char FunctionAnalysisManagerModuleProxy::PassID; |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 148 | |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 149 | FunctionAnalysisManagerModuleProxy::Result |
| 150 | FunctionAnalysisManagerModuleProxy::run(Module *M) { |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 151 | assert(FAM.empty() && "Function analyses ran prior to the module proxy!"); |
| 152 | return Result(FAM); |
| 153 | } |
| 154 | |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 155 | FunctionAnalysisManagerModuleProxy::Result::~Result() { |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 156 | // Clear out the analysis manager if we're being destroyed -- it means we |
| 157 | // didn't even see an invalidate call when we got invalidated. |
| 158 | FAM.clear(); |
| 159 | } |
| 160 | |
Chandler Carruth | b3e7219 | 2013-11-22 00:43:29 +0000 | [diff] [blame] | 161 | bool FunctionAnalysisManagerModuleProxy::Result::invalidate( |
| 162 | Module *M, const PreservedAnalyses &PA) { |
Chandler Carruth | bceeb22 | 2013-11-22 23:38:07 +0000 | [diff] [blame] | 163 | // If this proxy isn't marked as preserved, then we can't even invalidate |
| 164 | // individual function analyses, there may be an invalid set of Function |
| 165 | // objects in the cache making it impossible to incrementally preserve them. |
| 166 | // Just clear the entire manager. |
| 167 | if (!PA.preserved(ID())) |
Chandler Carruth | 2846e9e | 2013-11-21 10:53:05 +0000 | [diff] [blame] | 168 | FAM.clear(); |
Chandler Carruth | 851a2aa | 2013-11-21 02:11:31 +0000 | [diff] [blame] | 169 | |
| 170 | // Return false to indicate that this result is still a valid proxy. |
| 171 | return false; |
| 172 | } |
Chandler Carruth | c1ff9ed | 2013-11-23 01:25:07 +0000 | [diff] [blame^] | 173 | |
| 174 | char ModuleAnalysisManagerFunctionProxy::PassID; |