LLVM API Documentation
00001 //===- llvm/PassManagers.h - Pass Infrastructure classes -------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file declares the LLVM Pass Manager infrastructure. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_PASSMANAGERS_H 00015 #define LLVM_PASSMANAGERS_H 00016 00017 #include "llvm/PassManager.h" 00018 #include "llvm/ADT/SmallVector.h" 00019 #include "llvm/ADT/SmallPtrSet.h" 00020 #include "llvm/ADT/DenseMap.h" 00021 #include <deque> 00022 #include <map> 00023 00024 //===----------------------------------------------------------------------===// 00025 // Overview: 00026 // The Pass Manager Infrastructure manages passes. It's responsibilities are: 00027 // 00028 // o Manage optimization pass execution order 00029 // o Make required Analysis information available before pass P is run 00030 // o Release memory occupied by dead passes 00031 // o If Analysis information is dirtied by a pass then regenerate Analysis 00032 // information before it is consumed by another pass. 00033 // 00034 // Pass Manager Infrastructure uses multiple pass managers. They are 00035 // PassManager, FunctionPassManager, MPPassManager, FPPassManager, BBPassManager. 00036 // This class hierarchy uses multiple inheritance but pass managers do not 00037 // derive from another pass manager. 00038 // 00039 // PassManager and FunctionPassManager are two top-level pass manager that 00040 // represents the external interface of this entire pass manager infrastucture. 00041 // 00042 // Important classes : 00043 // 00044 // [o] class PMTopLevelManager; 00045 // 00046 // Two top level managers, PassManager and FunctionPassManager, derive from 00047 // PMTopLevelManager. PMTopLevelManager manages information used by top level 00048 // managers such as last user info. 00049 // 00050 // [o] class PMDataManager; 00051 // 00052 // PMDataManager manages information, e.g. list of available analysis info, 00053 // used by a pass manager to manage execution order of passes. It also provides 00054 // a place to implement common pass manager APIs. All pass managers derive from 00055 // PMDataManager. 00056 // 00057 // [o] class BBPassManager : public FunctionPass, public PMDataManager; 00058 // 00059 // BBPassManager manages BasicBlockPasses. 00060 // 00061 // [o] class FunctionPassManager; 00062 // 00063 // This is a external interface used by JIT to manage FunctionPasses. This 00064 // interface relies on FunctionPassManagerImpl to do all the tasks. 00065 // 00066 // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager, 00067 // public PMTopLevelManager; 00068 // 00069 // FunctionPassManagerImpl is a top level manager. It manages FPPassManagers 00070 // 00071 // [o] class FPPassManager : public ModulePass, public PMDataManager; 00072 // 00073 // FPPassManager manages FunctionPasses and BBPassManagers 00074 // 00075 // [o] class MPPassManager : public Pass, public PMDataManager; 00076 // 00077 // MPPassManager manages ModulePasses and FPPassManagers 00078 // 00079 // [o] class PassManager; 00080 // 00081 // This is a external interface used by various tools to manages passes. It 00082 // relies on PassManagerImpl to do all the tasks. 00083 // 00084 // [o] class PassManagerImpl : public Pass, public PMDataManager, 00085 // public PMDTopLevelManager 00086 // 00087 // PassManagerImpl is a top level pass manager responsible for managing 00088 // MPPassManagers. 00089 //===----------------------------------------------------------------------===// 00090 00091 namespace llvm { 00092 00093 /// FunctionPassManager and PassManager, two top level managers, serve 00094 /// as the public interface of pass manager infrastructure. 00095 enum TopLevelManagerType { 00096 TLM_Function, // FunctionPassManager 00097 TLM_Pass // PassManager 00098 }; 00099 00100 // enums for debugging strings 00101 enum PassDebuggingString { 00102 EXECUTION_MSG, // "Executing Pass '" 00103 MODIFICATION_MSG, // "' Made Modification '" 00104 FREEING_MSG, // " Freeing Pass '" 00105 ON_BASICBLOCK_MSG, // "' on BasicBlock '" + PassName + "'...\n" 00106 ON_FUNCTION_MSG, // "' on Function '" + FunctionName + "'...\n" 00107 ON_MODULE_MSG, // "' on Module '" + ModuleName + "'...\n" 00108 ON_LOOP_MSG, // " 'on Loop ...\n'" 00109 ON_CG_MSG // "' on Call Graph ...\n'" 00110 }; 00111 00112 //===----------------------------------------------------------------------===// 00113 // PMStack 00114 // 00115 /// PMStack 00116 /// Top level pass managers (see PassManager.cpp) maintain active Pass Managers 00117 /// using PMStack. Each Pass implements assignPassManager() to connect itself 00118 /// with appropriate manager. assignPassManager() walks PMStack to find 00119 /// suitable manager. 00120 /// 00121 /// PMStack is just a wrapper around standard deque that overrides pop() and 00122 /// push() methods. 00123 class PMStack { 00124 public: 00125 typedef std::deque<PMDataManager *>::reverse_iterator iterator; 00126 iterator begin() { return S.rbegin(); } 00127 iterator end() { return S.rend(); } 00128 00129 void handleLastUserOverflow(); 00130 00131 void pop(); 00132 inline PMDataManager *top() { return S.back(); } 00133 void push(PMDataManager *PM); 00134 inline bool empty() { return S.empty(); } 00135 00136 void dump(); 00137 private: 00138 std::deque<PMDataManager *> S; 00139 }; 00140 00141 00142 //===----------------------------------------------------------------------===// 00143 // PMTopLevelManager 00144 // 00145 /// PMTopLevelManager manages LastUser info and collects common APIs used by 00146 /// top level pass managers. 00147 class PMTopLevelManager { 00148 public: 00149 00150 virtual unsigned getNumContainedManagers() const { 00151 return (unsigned)PassManagers.size(); 00152 } 00153 00154 /// Schedule pass P for execution. Make sure that passes required by 00155 /// P are run before P is run. Update analysis info maintained by 00156 /// the manager. Remove dead passes. This is a recursive function. 00157 void schedulePass(Pass *P); 00158 00159 /// This is implemented by top level pass manager and used by 00160 /// schedulePass() to add analysis info passes that are not available. 00161 virtual void addTopLevelPass(Pass *P) = 0; 00162 00163 /// Set pass P as the last user of the given analysis passes. 00164 void setLastUser(SmallVector<Pass *, 12> &AnalysisPasses, Pass *P); 00165 00166 /// Collect passes whose last user is P 00167 void collectLastUses(SmallVector<Pass *, 12> &LastUses, Pass *P); 00168 00169 /// Find the pass that implements Analysis AID. Search immutable 00170 /// passes and all pass managers. If desired pass is not found 00171 /// then return NULL. 00172 Pass *findAnalysisPass(AnalysisID AID); 00173 00174 /// Find analysis usage information for the pass P. 00175 AnalysisUsage *findAnalysisUsage(Pass *P); 00176 00177 explicit PMTopLevelManager(enum TopLevelManagerType t); 00178 virtual ~PMTopLevelManager(); 00179 00180 /// Add immutable pass and initialize it. 00181 inline void addImmutablePass(ImmutablePass *P) { 00182 P->initializePass(); 00183 ImmutablePasses.push_back(P); 00184 } 00185 00186 inline SmallVector<ImmutablePass *, 8>& getImmutablePasses() { 00187 return ImmutablePasses; 00188 } 00189 00190 void addPassManager(PMDataManager *Manager) { 00191 PassManagers.push_back(Manager); 00192 } 00193 00194 // Add Manager into the list of managers that are not directly 00195 // maintained by this top level pass manager 00196 inline void addIndirectPassManager(PMDataManager *Manager) { 00197 IndirectPassManagers.push_back(Manager); 00198 } 00199 00200 // Print passes managed by this top level manager. 00201 void dumpPasses() const; 00202 void dumpArguments() const; 00203 00204 void initializeAllAnalysisInfo(); 00205 00206 // Active Pass Managers 00207 PMStack activeStack; 00208 00209 protected: 00210 00211 /// Collection of pass managers 00212 SmallVector<PMDataManager *, 8> PassManagers; 00213 00214 private: 00215 00216 /// Collection of pass managers that are not directly maintained 00217 /// by this pass manager 00218 SmallVector<PMDataManager *, 8> IndirectPassManagers; 00219 00220 // Map to keep track of last user of the analysis pass. 00221 // LastUser->second is the last user of Lastuser->first. 00222 DenseMap<Pass *, Pass *> LastUser; 00223 00224 // Map to keep track of passes that are last used by a pass. 00225 // This inverse map is initialized at PM->run() based on 00226 // LastUser map. 00227 DenseMap<Pass *, SmallPtrSet<Pass *, 8> > InversedLastUser; 00228 00229 /// Immutable passes are managed by top level manager. 00230 SmallVector<ImmutablePass *, 8> ImmutablePasses; 00231 00232 DenseMap<Pass *, AnalysisUsage *> AnUsageMap; 00233 }; 00234 00235 00236 00237 //===----------------------------------------------------------------------===// 00238 // PMDataManager 00239 00240 /// PMDataManager provides the common place to manage the analysis data 00241 /// used by pass managers. 00242 class PMDataManager { 00243 public: 00244 00245 explicit PMDataManager(int Depth) : TPM(NULL), Depth(Depth) { 00246 initializeAnalysisInfo(); 00247 } 00248 00249 virtual ~PMDataManager(); 00250 00251 /// Augment AvailableAnalysis by adding analysis made available by pass P. 00252 void recordAvailableAnalysis(Pass *P); 00253 00254 /// verifyPreservedAnalysis -- Verify analysis presreved by pass P. 00255 void verifyPreservedAnalysis(Pass *P); 00256 00257 /// verifyDomInfo -- Verify dominator information if it is available. 00258 void verifyDomInfo(Pass &P, Function &F); 00259 00260 /// Remove Analysis that is not preserved by the pass 00261 void removeNotPreservedAnalysis(Pass *P); 00262 00263 /// Remove dead passes 00264 void removeDeadPasses(Pass *P, const char *Msg, enum PassDebuggingString); 00265 00266 /// Add pass P into the PassVector. Update 00267 /// AvailableAnalysis appropriately if ProcessAnalysis is true. 00268 void add(Pass *P, bool ProcessAnalysis = true); 00269 00270 /// Add RequiredPass into list of lower level passes required by pass P. 00271 /// RequiredPass is run on the fly by Pass Manager when P requests it 00272 /// through getAnalysis interface. 00273 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass); 00274 00275 virtual Pass * getOnTheFlyPass(Pass *P, const PassInfo *PI, Function &F) { 00276 assert (0 && "Unable to find on the fly pass"); 00277 return NULL; 00278 } 00279 00280 /// Initialize available analysis information. 00281 void initializeAnalysisInfo() { 00282 AvailableAnalysis.clear(); 00283 for (unsigned i = 0; i < PMT_Last; ++i) 00284 InheritedAnalysis[i] = NULL; 00285 } 00286 00287 // Return true if P preserves high level analysis used by other 00288 // passes that are managed by this manager. 00289 bool preserveHigherLevelAnalysis(Pass *P); 00290 00291 00292 /// Populate RequiredPasses with analysis pass that are required by 00293 /// pass P and are available. Populate ReqPassNotAvailable with analysis 00294 /// pass that are required by pass P but are not available. 00295 void collectRequiredAnalysis(SmallVector<Pass *, 8> &RequiredPasses, 00296 SmallVector<AnalysisID, 8> &ReqPassNotAvailable, 00297 Pass *P); 00298 00299 /// All Required analyses should be available to the pass as it runs! Here 00300 /// we fill in the AnalysisImpls member of the pass so that it can 00301 /// successfully use the getAnalysis() method to retrieve the 00302 /// implementations it needs. 00303 void initializeAnalysisImpl(Pass *P); 00304 00305 /// Find the pass that implements Analysis AID. If desired pass is not found 00306 /// then return NULL. 00307 Pass *findAnalysisPass(AnalysisID AID, bool Direction); 00308 00309 // Access toplevel manager 00310 PMTopLevelManager *getTopLevelManager() { return TPM; } 00311 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; } 00312 00313 unsigned getDepth() const { return Depth; } 00314 00315 // Print routines used by debug-pass 00316 void dumpLastUses(Pass *P, unsigned Offset) const; 00317 void dumpPassArguments() const; 00318 void dumpPassInfo(Pass *P, enum PassDebuggingString S1, 00319 enum PassDebuggingString S2, const char *Msg); 00320 void dumpRequiredSet(const Pass *P) const; 00321 void dumpPreservedSet(const Pass *P) const; 00322 00323 virtual unsigned getNumContainedPasses() const { 00324 return (unsigned)PassVector.size(); 00325 } 00326 00327 virtual PassManagerType getPassManagerType() const { 00328 assert ( 0 && "Invalid use of getPassManagerType"); 00329 return PMT_Unknown; 00330 } 00331 00332 std::map<AnalysisID, Pass*> *getAvailableAnalysis() { 00333 return &AvailableAnalysis; 00334 } 00335 00336 // Collect AvailableAnalysis from all the active Pass Managers. 00337 void populateInheritedAnalysis(PMStack &PMS) { 00338 unsigned Index = 0; 00339 for (PMStack::iterator I = PMS.begin(), E = PMS.end(); 00340 I != E; ++I) 00341 InheritedAnalysis[Index++] = (*I)->getAvailableAnalysis(); 00342 } 00343 00344 protected: 00345 00346 // Top level manager. 00347 PMTopLevelManager *TPM; 00348 00349 // Collection of pass that are managed by this manager 00350 SmallVector<Pass *, 16> PassVector; 00351 00352 // Collection of Analysis provided by Parent pass manager and 00353 // used by current pass manager. At at time there can not be more 00354 // then PMT_Last active pass mangers. 00355 std::map<AnalysisID, Pass *> *InheritedAnalysis[PMT_Last]; 00356 00357 private: 00358 void dumpAnalysisUsage(const char *Msg, const Pass *P, 00359 const AnalysisUsage::VectorType &Set) const; 00360 00361 // Set of available Analysis. This information is used while scheduling 00362 // pass. If a pass requires an analysis which is not not available then 00363 // equired analysis pass is scheduled to run before the pass itself is 00364 // scheduled to run. 00365 std::map<AnalysisID, Pass*> AvailableAnalysis; 00366 00367 // Collection of higher level analysis used by the pass managed by 00368 // this manager. 00369 SmallVector<Pass *, 8> HigherLevelAnalysis; 00370 00371 unsigned Depth; 00372 }; 00373 00374 //===----------------------------------------------------------------------===// 00375 // FPPassManager 00376 // 00377 /// FPPassManager manages BBPassManagers and FunctionPasses. 00378 /// It batches all function passes and basic block pass managers together and 00379 /// sequence them to process one function at a time before processing next 00380 /// function. 00381 00382 class FPPassManager : public ModulePass, public PMDataManager { 00383 00384 public: 00385 static char ID; 00386 explicit FPPassManager(int Depth) 00387 : ModulePass(intptr_t(&ID)), PMDataManager(Depth) { } 00388 00389 /// run - Execute all of the passes scheduled for execution. Keep track of 00390 /// whether any of the passes modifies the module, and if so, return true. 00391 bool runOnFunction(Function &F); 00392 bool runOnModule(Module &M); 00393 00394 /// doInitialization - Run all of the initializers for the function passes. 00395 /// 00396 bool doInitialization(Module &M); 00397 00398 /// doFinalization - Run all of the finalizers for the function passes. 00399 /// 00400 bool doFinalization(Module &M); 00401 00402 /// Pass Manager itself does not invalidate any analysis info. 00403 void getAnalysisUsage(AnalysisUsage &Info) const { 00404 Info.setPreservesAll(); 00405 } 00406 00407 // Print passes managed by this manager 00408 void dumpPassStructure(unsigned Offset); 00409 00410 virtual const char *getPassName() const { 00411 return "Function Pass Manager"; 00412 } 00413 00414 FunctionPass *getContainedPass(unsigned N) { 00415 assert ( N < PassVector.size() && "Pass number out of range!"); 00416 FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]); 00417 return FP; 00418 } 00419 00420 virtual PassManagerType getPassManagerType() const { 00421 return PMT_FunctionPassManager; 00422 } 00423 }; 00424 00425 } 00426 00427 extern void StartPassTimer(llvm::Pass *); 00428 extern void StopPassTimer(llvm::Pass *); 00429 00430 #endif
This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.