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/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(llvm_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 llvm::tie(RI, Inserted) = ModuleAnalysisResults.insert(std::make_pair( 48 PassID, polymorphic_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 = 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 = ModuleAnalysisResults.find(PassID); 61 return RI == ModuleAnalysisResults.end() ? 0 : &*RI->second; 62 } 63 64 void ModuleAnalysisManager::invalidateImpl(void *PassID, Module *M) { 65 ModuleAnalysisResults.erase(PassID); 66 } 67 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 79 PreservedAnalyses FunctionPassManager::run(Function *F, FunctionAnalysisManager *AM) { 80 PreservedAnalyses PA = PreservedAnalyses::all(); 81 82 if (DebugPM) 83 dbgs() << "Starting function pass manager run.\n"; 84 85 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) { 86 if (DebugPM) 87 dbgs() << "Running function pass: " << Passes[Idx]->name() << "\n"; 88 89 PreservedAnalyses PassPA = Passes[Idx]->run(F, AM); 90 if (AM) 91 AM->invalidate(F, PassPA); 92 PA.intersect(llvm_move(PassPA)); 93 } 94 95 if (DebugPM) 96 dbgs() << "Finished function pass manager run.\n"; 97 98 return PA; 99 } 100 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 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 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) { 150 // Clear all the invalidated results associated specifically with this 151 // function. 152 SmallVector<void *, 8> InvalidatedPassIDs; 153 FunctionAnalysisResultListT &ResultsList = FunctionAnalysisResultLists[F]; 154 for (FunctionAnalysisResultListT::iterator I = ResultsList.begin(), 155 E = ResultsList.end(); 156 I != E;) 157 if (I->second->invalidate(F, PA)) { 158 InvalidatedPassIDs.push_back(I->first); 159 I = ResultsList.erase(I); 160 } else { 161 ++I; 162 } 163 while (!InvalidatedPassIDs.empty()) 164 FunctionAnalysisResults.erase( 165 std::make_pair(InvalidatedPassIDs.pop_back_val(), F)); 166 } 167 168 char FunctionAnalysisManagerModuleProxy::PassID; 169 170 FunctionAnalysisManagerModuleProxy::Result 171 FunctionAnalysisManagerModuleProxy::run(Module *M) { 172 assert(FAM.empty() && "Function analyses ran prior to the module proxy!"); 173 return Result(FAM); 174 } 175 176 FunctionAnalysisManagerModuleProxy::Result::~Result() { 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 182 bool FunctionAnalysisManagerModuleProxy::Result::invalidate( 183 Module *M, const PreservedAnalyses &PA) { 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())) 189 FAM.clear(); 190 191 // Return false to indicate that this result is still a valid proxy. 192 return false; 193 } 194 195 char ModuleAnalysisManagerFunctionProxy::PassID; 196