You are on page 1of 8

LLVM API Documentation

Main Page Related Pages Modules Namespaces Classes Files Directories File List File Members llvm include llvm

PassManagers.h
Go to the documentation of this file.
00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 00023 00024 00025 00026 00027 00028 00029 00030 00031 00032 00033 00034 00035 00036 00037 00038 00039 //===- llvm/PassManagers.h - Pass Infrastructure classes -------*- C++ -*-===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // // This file declares the LLVM Pass Manager infrastructure. // //===----------------------------------------------------------------------===// #ifndef LLVM_PASSMANAGERS_H #define LLVM_PASSMANAGERS_H #include #include #include #include #include #include "llvm/Pass.h" "llvm/ADT/SmallVector.h" "llvm/ADT/SmallPtrSet.h" "llvm/ADT/DenseMap.h" <vector> <map>

//===----------------------------------------------------------------------===// // Overview: // The Pass Manager Infrastructure manages passes. It's responsibilities are: // // o Manage optimization pass execution order // o Make required Analysis information available before pass P is run // o Release memory occupied by dead passes // o If Analysis information is dirtied by a pass then regenerate Analysis // information before it is consumed by another pass. // // Pass Manager Infrastructure uses multiple pass managers. They are // PassManager, FunctionPassManager, MPPassManager, FPPassManager, BBPassManager. // This class hierarchy uses multiple inheritance but pass managers do not // derive from another pass manager. // // PassManager and FunctionPassManager are two top-level pass manager that

00040 00041 00042 00043 00044 00045 00046 00047 00048 00049 00050 00051 00052 00053 00054 00055 00056 00057 00058 00059 00060 00061 00062 00063 00064 00065 00066 00067 00068 00069 00070 00071 00072 00073 00074 00075 00076 00077 00078 00079 00080 00081 00082 00083 00084 00085 00086 00087 00088 00089 00090 00091 00092 00093 00094 00095 00096 00097 00098 00099 00100 00101 00102 00103 00104 00105

// represents the external interface of this entire pass manager infrastucture. // // Important classes : // // [o] class PMTopLevelManager; // // Two top level managers, PassManager and FunctionPassManager, derive from // PMTopLevelManager. PMTopLevelManager manages information used by top level // managers such as last user info. // // [o] class PMDataManager; // // PMDataManager manages information, e.g. list of available analysis info, // used by a pass manager to manage execution order of passes. It also provides // a place to implement common pass manager APIs. All pass managers derive from // PMDataManager. // // [o] class BBPassManager : public FunctionPass, public PMDataManager; // // BBPassManager manages BasicBlockPasses. // // [o] class FunctionPassManager; // // This is a external interface used by JIT to manage FunctionPasses. This // interface relies on FunctionPassManagerImpl to do all the tasks. // // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager, // public PMTopLevelManager; // // FunctionPassManagerImpl is a top level manager. It manages FPPassManagers // // [o] class FPPassManager : public ModulePass, public PMDataManager; // // FPPassManager manages FunctionPasses and BBPassManagers // // [o] class MPPassManager : public Pass, public PMDataManager; // // MPPassManager manages ModulePasses and FPPassManagers // // [o] class PassManager; // // This is a external interface used by various tools to manages passes. It // relies on PassManagerImpl to do all the tasks. // // [o] class PassManagerImpl : public Pass, public PMDataManager, // public PMTopLevelManager // // PassManagerImpl is a top level pass manager responsible for managing // MPPassManagers. //===----------------------------------------------------------------------===// #include "llvm/Support/PrettyStackTrace.h" namespace llvm { class Module; class Pass; class StringRef; class Value; class Timer; class PMDataManager; // enums for debugging strings enum PassDebuggingString { EXECUTION_MSG, // "Executing Pass '" MODIFICATION_MSG, // "' Made Modification '" FREEING_MSG, // " Freeing Pass '"

00106 00107 00108 00109 00110 00111 00112 00113 00114 00115 00116 00117 00118 00119 00120 00121 00122 00123 00124 00125 00126 00127 00128 00129 00130 00131 00132 00133 00134 00135 00136 00137 00138 00139 00140 00141 00142 00143 00144 00145 00146 00147 00148 00149 00150 00151 00152 00153 00154 00155 00156 00157 00158 00159 00160 00161 00162 00163 00164 00165 00166 00167 00168 00169 00170 00171

ON_BASICBLOCK_MSG, // "' on BasicBlock '" + PassName + "'...\n" ON_FUNCTION_MSG, // "' on Function '" + FunctionName + "'...\n" ON_MODULE_MSG, // "' on Module '" + ModuleName + "'...\n" ON_REGION_MSG, // " 'on Region ...\n'" ON_LOOP_MSG, // " 'on Loop ...\n'" ON_CG_MSG // "' on Call Graph ...\n'" }; /// PassManagerPrettyStackEntry - This is used to print informative information /// about what pass is running when/if a stack trace is generated. class PassManagerPrettyStackEntry : public PrettyStackTraceEntry { Pass *P; Value *V; Module *M; public: explicit PassManagerPrettyStackEntry(Pass *p) : P(p), V(0), M(0) {} // When P is releaseMemory'd. PassManagerPrettyStackEntry(Pass *p, Value &v) : P(p), V(&v), M(0) {} // When P is run on V PassManagerPrettyStackEntry(Pass *p, Module &m) : P(p), V(0), M(&m) {} // When P is run on M /// print - Emit information about this stack frame to OS. virtual void print(raw_ostream &OS) const; }; //===----------------------------------------------------------------------===// // PMStack // /// PMStack - This class implements a stack data structure of PMDataManager /// pointers. /// /// Top level pass managers (see PassManager.cpp) maintain active Pass Managers /// using PMStack. Each Pass implements assignPassManager() to connect itself /// with appropriate manager. assignPassManager() walks PMStack to find /// suitable manager. class PMStack { public: typedef std::vector<PMDataManager *>::const_reverse_iterator iterator; iterator begin() const { return S.rbegin(); } iterator end() const { return S.rend(); } void pop(); PMDataManager *top() const { return S.back(); } void push(PMDataManager *PM); bool empty() const { return S.empty(); } void dump() const; private: std::vector<PMDataManager *> S; }; //===----------------------------------------------------------------------===// // PMTopLevelManager // /// PMTopLevelManager manages LastUser info and collects common APIs used by /// top level pass managers. class PMTopLevelManager { protected: explicit PMTopLevelManager(PMDataManager *PMDM); virtual unsigned getNumContainedManagers() const { return (unsigned)PassManagers.size();

00172 00173 00174 00175 00176 00177 00178 00179 00180 00181 00182 00183 00184 00185 00186 00187 00188 00189 00190 00191 00192 00193 00194 00195 00196 00197 00198 00199 00200 00201 00202 00203 00204 00205 00206 00207 00208 00209 00210 00211 00212 00213 00214 00215 00216 00217 00218 00219 00220 00221 00222 00223 00224 00225 00226 00227 00228 00229 00230 00231 00232 00233 00234 00235 00236 00237

} void initializeAllAnalysisInfo(); private: virtual PMDataManager *getAsPMDataManager() = 0; virtual PassManagerType getTopLevelPassManagerType() = 0; public: /// Schedule pass P for execution. Make sure that passes required by /// P are run before P is run. Update analysis info maintained by /// the manager. Remove dead passes. This is a recursive function. void schedulePass(Pass *P); /// Set pass P as the last user of the given analysis passes. void setLastUser(const SmallVectorImpl<Pass *> &AnalysisPasses, Pass *P); /// Collect passes whose last user is P void collectLastUses(SmallVectorImpl<Pass *> &LastUses, Pass *P); /// Find the pass that implements Analysis AID. Search immutable /// passes and all pass managers. If desired pass is not found /// then return NULL. Pass *findAnalysisPass(AnalysisID AID); /// Find analysis usage information for the pass P. AnalysisUsage *findAnalysisUsage(Pass *P); virtual ~PMTopLevelManager(); /// Add immutable pass and initialize it. inline void addImmutablePass(ImmutablePass *P) { P->initializePass(); ImmutablePasses.push_back(P); } inline SmallVectorImpl<ImmutablePass *>& getImmutablePasses() { return ImmutablePasses; } void addPassManager(PMDataManager *Manager) { PassManagers.push_back(Manager); } // Add Manager into the list of managers that are not directly // maintained by this top level pass manager inline void addIndirectPassManager(PMDataManager *Manager) { IndirectPassManagers.push_back(Manager); } // Print passes managed by this top level manager. void dumpPasses() const; void dumpArguments() const; // Active Pass Managers PMStack activeStack; protected: /// Collection of pass managers SmallVector<PMDataManager *, 8> PassManagers; private: /// Collection of pass managers that are not directly maintained /// by this pass manager

00238 00239 00240 00241 00242 00243 00244 00245 00246 00247 00248 00249 00250 00251 00252 00253 00254 00255 00256 00257 00258 00259 00260 00261 00262 00263 00264 00265 00266 00267 00268 00269 00270 00271 00272 00273 00274 00275 00276 00277 00278 00279 00280 00281 00282 00283 00284 00285 00286 00287 00288 00289 00290 00291 00292 00293 00294 00295 00296 00297 00298 00299 00300 00301 00302 00303

SmallVector<PMDataManager *, 8> IndirectPassManagers; // Map to keep track of last user of the analysis pass. // LastUser->second is the last user of Lastuser->first. DenseMap<Pass *, Pass *> LastUser; // Map to keep track of passes that are last used by a pass. // This inverse map is initialized at PM->run() based on // LastUser map. DenseMap<Pass *, SmallPtrSet<Pass *, 8> > InversedLastUser; /// Immutable passes are managed by top level manager. SmallVector<ImmutablePass *, 8> ImmutablePasses; DenseMap<Pass *, AnalysisUsage *> AnUsageMap; };

//===----------------------------------------------------------------------===// // PMDataManager /// PMDataManager provides the common place to manage the analysis data /// used by pass managers. class PMDataManager { public: explicit PMDataManager() : TPM(NULL), Depth(0) { initializeAnalysisInfo(); } virtual ~PMDataManager(); virtual Pass *getAsPass() = 0; /// Augment AvailableAnalysis by adding analysis made available by pass P. void recordAvailableAnalysis(Pass *P); /// verifyPreservedAnalysis -- Verify analysis presreved by pass P. void verifyPreservedAnalysis(Pass *P); /// Remove Analysis that is not preserved by the pass void removeNotPreservedAnalysis(Pass *P); /// Remove dead passes used by P. void removeDeadPasses(Pass *P, StringRef Msg, enum PassDebuggingString); /// Remove P. void freePass(Pass *P, StringRef Msg, enum PassDebuggingString); /// Add pass P into the PassVector. Update /// AvailableAnalysis appropriately if ProcessAnalysis is true. void add(Pass *P, bool ProcessAnalysis = true); /// Add RequiredPass into list of lower level passes required by pass P. /// RequiredPass is run on the fly by Pass Manager when P requests it /// through getAnalysis interface. virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass); virtual Pass *getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F); /// Initialize available analysis information. void initializeAnalysisInfo() { AvailableAnalysis.clear();

00304 for (unsigned i = 0; i < PMT_Last; ++i) 00305 InheritedAnalysis[i] = NULL; 00306 } 00307 00308 // Return true if P preserves high level analysis used by other 00309 // passes that are managed by this manager. 00310 bool preserveHigherLevelAnalysis(Pass *P); 00311 00312 00313 /// Populate RequiredPasses with analysis pass that are required by 00314 /// pass P and are available. Populate ReqPassNotAvailable with analysis 00315 /// pass that are required by pass P but are not available. 00316 void collectRequiredAnalysis(SmallVectorImpl<Pass *> &RequiredPasses, 00317 SmallVectorImpl<AnalysisID> &ReqPassNotAvailable, 00318 Pass *P); 00319 00320 /// All Required analyses should be available to the pass as it runs! Here 00321 /// we fill in the AnalysisImpls member of the pass so that it can 00322 /// successfully use the getAnalysis() method to retrieve the 00323 /// implementations it needs. 00324 void initializeAnalysisImpl(Pass *P); 00325 00326 /// Find the pass that implements Analysis AID. If desired pass is not found 00327 /// then return NULL. 00328 Pass *findAnalysisPass(AnalysisID AID, bool Direction); 00329 00330 // Access toplevel manager 00331 PMTopLevelManager *getTopLevelManager() { return TPM; } 00332 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; } 00333 00334 unsigned getDepth() const { return Depth; } 00335 void setDepth(unsigned newDepth) { Depth = newDepth; } 00336 00337 // Print routines used by debug-pass 00338 void dumpLastUses(Pass *P, unsigned Offset) const; 00339 void dumpPassArguments() const; 00340 void dumpPassInfo(Pass *P, enum PassDebuggingString S1, 00341 enum PassDebuggingString S2, StringRef Msg); 00342 void dumpRequiredSet(const Pass *P) const; 00343 void dumpPreservedSet(const Pass *P) const; 00344 00345 virtual unsigned getNumContainedPasses() const { 00346 return (unsigned)PassVector.size(); 00347 } 00348 00349 virtual PassManagerType getPassManagerType() const { 00350 assert ( 0 && "Invalid use of getPassManagerType"); 00351 return PMT_Unknown; 00352 } 00353 00354 std::map<AnalysisID, Pass*> *getAvailableAnalysis() { 00355 return &AvailableAnalysis; 00356 } 00357 00358 // Collect AvailableAnalysis from all the active Pass Managers. 00359 void populateInheritedAnalysis(PMStack &PMS) { 00360 unsigned Index = 0; 00361 for (PMStack::iterator I = PMS.begin(), E = PMS.end(); 00362 I != E; ++I) 00363 InheritedAnalysis[Index++] = (*I)->getAvailableAnalysis(); 00364 } 00365 00366 protected: 00367 00368 // Top level manager. 00369 PMTopLevelManager *TPM;

00370 00371 00372 00373 00374 00375 00376 00377 00378 00379 00380 00381 00382 00383 00384 00385 00386 00387 00388 00389 00390 00391 00392 00393 00394 00395 00396 00397 00398 00399 00400 00401 00402 00403 00404 00405 00406 00407 00408 00409 00410 00411 00412 00413 00414 00415 00416 00417 00418 00419 00420 00421 00422 00423 00424 00425 00426 00427 00428 00429 00430 00431 00432 00433 00434 00435

// Collection of pass that are managed by this manager SmallVector<Pass *, 16> PassVector; // Collection of Analysis provided by Parent pass manager and // used by current pass manager. At at time there can not be more // then PMT_Last active pass mangers. std::map<AnalysisID, Pass *> *InheritedAnalysis[PMT_Last]; /// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions /// or higher is specified. bool isPassDebuggingExecutionsOrMore() const; private: void dumpAnalysisUsage(StringRef Msg, const Pass *P, const AnalysisUsage::VectorType &Set) const; // Set of available Analysis. This information is used while scheduling // pass. If a pass requires an analysis which is not available then // the required analysis pass is scheduled to run before the pass itself is // scheduled to run. std::map<AnalysisID, Pass*> AvailableAnalysis; // Collection of higher level analysis used by the pass managed by // this manager. SmallVector<Pass *, 8> HigherLevelAnalysis; unsigned Depth; }; //===----------------------------------------------------------------------===// // FPPassManager // /// FPPassManager manages BBPassManagers and FunctionPasses. /// It batches all function passes and basic block pass managers together and /// sequence them to process one function at a time before processing next /// function. class FPPassManager : public ModulePass, public PMDataManager { public: static char ID; explicit FPPassManager() : ModulePass(ID), PMDataManager() { } /// run - Execute all of the passes scheduled for execution. Keep track of /// whether any of the passes modifies the module, and if so, return true. bool runOnFunction(Function &F); bool runOnModule(Module &M); /// cleanup - After running all passes, clean up pass manager cache. void cleanup(); /// doInitialization - Run all of the initializers for the function passes. /// bool doInitialization(Module &M); /// doFinalization - Run all of the finalizers for the function passes. /// bool doFinalization(Module &M); virtual PMDataManager *getAsPMDataManager() { return this; } virtual Pass *getAsPass() { return this; } /// Pass Manager itself does not invalidate any analysis info. void getAnalysisUsage(AnalysisUsage &Info) const { Info.setPreservesAll();

00436 00437 00438 00439 00440 00441 00442 00443 00444 00445 00446 00447 00448 00449 00450 00451 00452 00453 00454 00455 00456 00457 00458 00459 00460

} // Print passes managed by this manager void dumpPassStructure(unsigned Offset); virtual const char *getPassName() const { return "Function Pass Manager"; } FunctionPass *getContainedPass(unsigned N) { assert ( N < PassVector.size() && "Pass number out of range!"); FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]); return FP; } virtual PassManagerType getPassManagerType() const { return PMT_FunctionPassManager; } }; Timer *getPassTimer(Pass *); } #endif
Generated on Wed Apr 11 2012 02:34:19 for LLVM by 1.7.5.1

Copyright 2003-2012 University of Illinois at Urbana-Champaign. All Rights Reserved.

You might also like