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