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 | |
| 15 | void ModulePassManager::run() { |
| 16 | for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) |
| 17 | if (Passes[Idx]->run(M)) |
| 18 | if (AM) AM->invalidateAll(M); |
| 19 | } |
| 20 | |
| 21 | bool FunctionPassManager::run(Module *M) { |
| 22 | bool Changed = false; |
| 23 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 24 | for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) |
| 25 | if (Passes[Idx]->run(I)) { |
| 26 | Changed = true; |
| 27 | if (AM) AM->invalidateAll(I); |
| 28 | } |
| 29 | return Changed; |
| 30 | } |
| 31 | |
| 32 | void AnalysisManager::invalidateAll(Function *F) { |
| 33 | assert(F->getParent() == M && "Invalidating a function from another module!"); |
| 34 | |
| 35 | // First invalidate any module results we still have laying about. |
| 36 | // FIXME: This is a total hack based on the fact that erasure doesn't |
| 37 | // invalidate iteration for DenseMap. |
| 38 | for (ModuleAnalysisResultMapT::iterator I = ModuleAnalysisResults.begin(), |
| 39 | E = ModuleAnalysisResults.end(); |
| 40 | I != E; ++I) |
| 41 | if (I->second->invalidate(M)) |
| 42 | ModuleAnalysisResults.erase(I); |
| 43 | |
| 44 | // Now clear all the invalidated results associated specifically with this |
| 45 | // function. |
| 46 | SmallVector<void *, 8> InvalidatedPassIDs; |
| 47 | FunctionAnalysisResultListT &ResultsList = FunctionAnalysisResultLists[F]; |
| 48 | for (FunctionAnalysisResultListT::iterator I = ResultsList.begin(), |
| 49 | E = ResultsList.end(); |
| 50 | I != E; ++I) |
| 51 | if (I->second->invalidate(F)) { |
| 52 | FunctionAnalysisResultListT::iterator Old = I--; |
| 53 | InvalidatedPassIDs.push_back(Old->first); |
| 54 | ResultsList.erase(Old); |
| 55 | } |
| 56 | while (!InvalidatedPassIDs.empty()) |
| 57 | FunctionAnalysisResults.erase( |
| 58 | std::make_pair(InvalidatedPassIDs.pop_back_val(), F)); |
| 59 | } |
| 60 | |
| 61 | void AnalysisManager::invalidateAll(Module *M) { |
| 62 | // First invalidate any module results we still have laying about. |
| 63 | // FIXME: This is a total hack based on the fact that erasure doesn't |
| 64 | // invalidate iteration for DenseMap. |
| 65 | for (ModuleAnalysisResultMapT::iterator I = ModuleAnalysisResults.begin(), |
| 66 | E = ModuleAnalysisResults.end(); |
| 67 | I != E; ++I) |
| 68 | if (I->second->invalidate(M)) |
| 69 | ModuleAnalysisResults.erase(I); |
| 70 | |
| 71 | // Now walk all of the functions for which there are cached results, and |
| 72 | // attempt to invalidate each of those as the entire module may have changed. |
| 73 | // FIXME: How do we handle functions which have been deleted or RAUWed? |
| 74 | SmallVector<void *, 8> InvalidatedPassIDs; |
| 75 | for (FunctionAnalysisResultListMapT::iterator |
| 76 | FI = FunctionAnalysisResultLists.begin(), |
| 77 | FE = FunctionAnalysisResultLists.end(); |
| 78 | FI != FE; ++FI) { |
| 79 | Function *F = FI->first; |
| 80 | FunctionAnalysisResultListT &ResultsList = FI->second; |
| 81 | for (FunctionAnalysisResultListT::iterator I = ResultsList.begin(), |
| 82 | E = ResultsList.end(); |
| 83 | I != E; ++I) |
| 84 | if (I->second->invalidate(F)) { |
| 85 | FunctionAnalysisResultListT::iterator Old = I--; |
| 86 | InvalidatedPassIDs.push_back(Old->first); |
| 87 | ResultsList.erase(Old); |
| 88 | } |
| 89 | while (!InvalidatedPassIDs.empty()) |
| 90 | FunctionAnalysisResults.erase( |
| 91 | std::make_pair(InvalidatedPassIDs.pop_back_val(), F)); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | const AnalysisManager::AnalysisResultConcept<Module> & |
| 96 | AnalysisManager::getResultImpl(void *PassID, Module *M) { |
| 97 | assert(M == this->M && "Wrong module used when querying the AnalysisManager"); |
| 98 | ModuleAnalysisResultMapT::iterator RI; |
| 99 | bool Inserted; |
| 100 | llvm::tie(RI, Inserted) = ModuleAnalysisResults.insert(std::make_pair( |
| 101 | PassID, polymorphic_ptr<AnalysisResultConcept<Module> >())); |
| 102 | |
| 103 | if (Inserted) { |
| 104 | // We don't have a cached result for this result. Look up the pass and run |
| 105 | // it to produce a result, which we then add to the cache. |
| 106 | ModuleAnalysisPassMapT::const_iterator PI = |
| 107 | ModuleAnalysisPasses.find(PassID); |
| 108 | assert(PI != ModuleAnalysisPasses.end() && |
| 109 | "Analysis passes must be registered prior to being queried!"); |
| 110 | RI->second = PI->second->run(M); |
| 111 | } |
| 112 | |
| 113 | return *RI->second; |
| 114 | } |
| 115 | |
| 116 | const AnalysisManager::AnalysisResultConcept<Function> & |
| 117 | AnalysisManager::getResultImpl(void *PassID, Function *F) { |
| 118 | assert(F->getParent() == M && "Analyzing a function from another module!"); |
| 119 | |
| 120 | FunctionAnalysisResultMapT::iterator RI; |
| 121 | bool Inserted; |
| 122 | llvm::tie(RI, Inserted) = FunctionAnalysisResults.insert(std::make_pair( |
| 123 | std::make_pair(PassID, F), FunctionAnalysisResultListT::iterator())); |
| 124 | |
| 125 | if (Inserted) { |
| 126 | // We don't have a cached result for this result. Look up the pass and run |
| 127 | // it to produce a result, which we then add to the cache. |
| 128 | FunctionAnalysisPassMapT::const_iterator PI = |
| 129 | FunctionAnalysisPasses.find(PassID); |
| 130 | assert(PI != FunctionAnalysisPasses.end() && |
| 131 | "Analysis passes must be registered prior to being queried!"); |
| 132 | FunctionAnalysisResultListT &ResultList = FunctionAnalysisResultLists[F]; |
| 133 | ResultList.push_back(std::make_pair(PassID, PI->second->run(F))); |
| 134 | RI->second = llvm::prior(ResultList.end()); |
| 135 | } |
| 136 | |
| 137 | return *RI->second->second; |
| 138 | } |
| 139 | |
| 140 | void AnalysisManager::invalidateImpl(void *PassID, Module *M) { |
| 141 | assert(M == this->M && "Invalidating a pass over a different module!"); |
| 142 | ModuleAnalysisResults.erase(PassID); |
| 143 | } |
| 144 | |
| 145 | void AnalysisManager::invalidateImpl(void *PassID, Function *F) { |
| 146 | assert(F->getParent() == M && |
| 147 | "Invalidating a pass over a function from another module!"); |
| 148 | |
Chandler Carruth | 1205a6c | 2013-11-15 21:44:35 +0000 | [diff] [blame^] | 149 | FunctionAnalysisResultMapT::iterator RI = |
| 150 | FunctionAnalysisResults.find(std::make_pair(PassID, F)); |
Chandler Carruth | 74015a7 | 2013-11-13 01:12:08 +0000 | [diff] [blame] | 151 | if (RI == FunctionAnalysisResults.end()) |
| 152 | return; |
| 153 | |
| 154 | FunctionAnalysisResultLists[F].erase(RI->second); |
| 155 | } |