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