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 | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame^] | 15 | void ModulePassManager::run(Module *M) { |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 16 | for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) |
| 17 | if (Passes[Idx]->run(M)) |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame^] | 18 | if (AM) |
| 19 | AM->invalidateAll(M); |
| 20 | } |
| 21 | |
| 22 | void ModuleAnalysisManager::invalidateAll(Module *M) { |
| 23 | // FIXME: This is a total hack based on the fact that erasure doesn't |
| 24 | // invalidate iteration for DenseMap. |
| 25 | for (ModuleAnalysisResultMapT::iterator I = ModuleAnalysisResults.begin(), |
| 26 | E = ModuleAnalysisResults.end(); |
| 27 | I != E; ++I) |
| 28 | if (I->second->invalidate(M)) |
| 29 | ModuleAnalysisResults.erase(I); |
| 30 | } |
| 31 | |
| 32 | const detail::AnalysisResultConcept<Module> & |
| 33 | ModuleAnalysisManager::getResultImpl(void *PassID, Module *M) { |
| 34 | ModuleAnalysisResultMapT::iterator RI; |
| 35 | bool Inserted; |
| 36 | llvm::tie(RI, Inserted) = ModuleAnalysisResults.insert(std::make_pair( |
| 37 | PassID, polymorphic_ptr<detail::AnalysisResultConcept<Module> >())); |
| 38 | |
| 39 | if (Inserted) { |
| 40 | // We don't have a cached result for this result. Look up the pass and run |
| 41 | // it to produce a result, which we then add to the cache. |
| 42 | ModuleAnalysisPassMapT::const_iterator PI = |
| 43 | ModuleAnalysisPasses.find(PassID); |
| 44 | assert(PI != ModuleAnalysisPasses.end() && |
| 45 | "Analysis passes must be registered prior to being queried!"); |
| 46 | RI->second = PI->second->run(M); |
| 47 | } |
| 48 | |
| 49 | return *RI->second; |
| 50 | } |
| 51 | |
| 52 | void ModuleAnalysisManager::invalidateImpl(void *PassID, Module *M) { |
| 53 | ModuleAnalysisResults.erase(PassID); |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | bool FunctionPassManager::run(Module *M) { |
| 57 | bool Changed = false; |
| 58 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 59 | for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) |
| 60 | if (Passes[Idx]->run(I)) { |
| 61 | Changed = true; |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame^] | 62 | if (AM) |
| 63 | AM->invalidateAll(I); |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 64 | } |
| 65 | return Changed; |
| 66 | } |
| 67 | |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame^] | 68 | void FunctionAnalysisManager::invalidateAll(Function *F) { |
| 69 | // Clear all the invalidated results associated specifically with this |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 70 | // function. |
| 71 | SmallVector<void *, 8> InvalidatedPassIDs; |
| 72 | FunctionAnalysisResultListT &ResultsList = FunctionAnalysisResultLists[F]; |
| 73 | for (FunctionAnalysisResultListT::iterator I = ResultsList.begin(), |
| 74 | E = ResultsList.end(); |
Chandler Carruth | 8c60bc9 | 2013-11-15 21:56:44 +0000 | [diff] [blame] | 75 | I != E;) |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 76 | if (I->second->invalidate(F)) { |
Chandler Carruth | 8c60bc9 | 2013-11-15 21:56:44 +0000 | [diff] [blame] | 77 | InvalidatedPassIDs.push_back(I->first); |
| 78 | I = ResultsList.erase(I); |
| 79 | } else { |
| 80 | ++I; |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 81 | } |
| 82 | while (!InvalidatedPassIDs.empty()) |
| 83 | FunctionAnalysisResults.erase( |
| 84 | std::make_pair(InvalidatedPassIDs.pop_back_val(), F)); |
| 85 | } |
| 86 | |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame^] | 87 | const detail::AnalysisResultConcept<Function> & |
| 88 | FunctionAnalysisManager::getResultImpl(void *PassID, Function *F) { |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 89 | FunctionAnalysisResultMapT::iterator RI; |
| 90 | bool Inserted; |
| 91 | llvm::tie(RI, Inserted) = FunctionAnalysisResults.insert(std::make_pair( |
| 92 | std::make_pair(PassID, F), FunctionAnalysisResultListT::iterator())); |
| 93 | |
| 94 | if (Inserted) { |
| 95 | // We don't have a cached result for this result. Look up the pass and run |
| 96 | // it to produce a result, which we then add to the cache. |
| 97 | FunctionAnalysisPassMapT::const_iterator PI = |
| 98 | FunctionAnalysisPasses.find(PassID); |
| 99 | assert(PI != FunctionAnalysisPasses.end() && |
| 100 | "Analysis passes must be registered prior to being queried!"); |
| 101 | FunctionAnalysisResultListT &ResultList = FunctionAnalysisResultLists[F]; |
| 102 | ResultList.push_back(std::make_pair(PassID, PI->second->run(F))); |
| 103 | RI->second = llvm::prior(ResultList.end()); |
| 104 | } |
| 105 | |
| 106 | return *RI->second->second; |
| 107 | } |
| 108 | |
Chandler Carruth | ed1ffe0 | 2013-11-20 04:01:38 +0000 | [diff] [blame^] | 109 | void FunctionAnalysisManager::invalidateImpl(void *PassID, Function *F) { |
Chandler Carruth | 1205a6c | 2013-11-15 21:44:35 +0000 | [diff] [blame] | 110 | FunctionAnalysisResultMapT::iterator RI = |
| 111 | FunctionAnalysisResults.find(std::make_pair(PassID, F)); |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 112 | if (RI == FunctionAnalysisResults.end()) |
| 113 | return; |
| 114 | |
| 115 | FunctionAnalysisResultLists[F].erase(RI->second); |
| 116 | } |