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