John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1 | //===- llvm/PassManagers.h - Pass Infrastructure classes -------*- C++ -*-===// |
| 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 | // This file declares the LLVM Pass Manager infrastructure. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_PASSMANAGERS_H |
| 15 | #define LLVM_PASSMANAGERS_H |
| 16 | |
| 17 | #include "llvm/Pass.h" |
| 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/ADT/SmallPtrSet.h" |
| 20 | #include "llvm/ADT/DenseMap.h" |
| 21 | #include <vector> |
| 22 | #include <map> |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | // Overview: |
| 26 | // The Pass Manager Infrastructure manages passes. It's responsibilities are: |
| 27 | // |
| 28 | // o Manage optimization pass execution order |
| 29 | // o Make required Analysis information available before pass P is run |
| 30 | // o Release memory occupied by dead passes |
| 31 | // o If Analysis information is dirtied by a pass then regenerate Analysis |
| 32 | // information before it is consumed by another pass. |
| 33 | // |
| 34 | // Pass Manager Infrastructure uses multiple pass managers. They are |
| 35 | // PassManager, FunctionPassManager, MPPassManager, FPPassManager, BBPassManager. |
| 36 | // This class hierarchy uses multiple inheritance but pass managers do not |
| 37 | // derive from another pass manager. |
| 38 | // |
| 39 | // PassManager and FunctionPassManager are two top-level pass manager that |
| 40 | // represents the external interface of this entire pass manager infrastucture. |
| 41 | // |
| 42 | // Important classes : |
| 43 | // |
| 44 | // [o] class PMTopLevelManager; |
| 45 | // |
| 46 | // Two top level managers, PassManager and FunctionPassManager, derive from |
| 47 | // PMTopLevelManager. PMTopLevelManager manages information used by top level |
| 48 | // managers such as last user info. |
| 49 | // |
| 50 | // [o] class PMDataManager; |
| 51 | // |
| 52 | // PMDataManager manages information, e.g. list of available analysis info, |
| 53 | // used by a pass manager to manage execution order of passes. It also provides |
| 54 | // a place to implement common pass manager APIs. All pass managers derive from |
| 55 | // PMDataManager. |
| 56 | // |
| 57 | // [o] class BBPassManager : public FunctionPass, public PMDataManager; |
| 58 | // |
| 59 | // BBPassManager manages BasicBlockPasses. |
| 60 | // |
| 61 | // [o] class FunctionPassManager; |
| 62 | // |
| 63 | // This is a external interface used by JIT to manage FunctionPasses. This |
| 64 | // interface relies on FunctionPassManagerImpl to do all the tasks. |
| 65 | // |
| 66 | // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager, |
| 67 | // public PMTopLevelManager; |
| 68 | // |
| 69 | // FunctionPassManagerImpl is a top level manager. It manages FPPassManagers |
| 70 | // |
| 71 | // [o] class FPPassManager : public ModulePass, public PMDataManager; |
| 72 | // |
| 73 | // FPPassManager manages FunctionPasses and BBPassManagers |
| 74 | // |
| 75 | // [o] class MPPassManager : public Pass, public PMDataManager; |
| 76 | // |
| 77 | // MPPassManager manages ModulePasses and FPPassManagers |
| 78 | // |
| 79 | // [o] class PassManager; |
| 80 | // |
| 81 | // This is a external interface used by various tools to manages passes. It |
| 82 | // relies on PassManagerImpl to do all the tasks. |
| 83 | // |
| 84 | // [o] class PassManagerImpl : public Pass, public PMDataManager, |
| 85 | // public PMDTopLevelManager |
| 86 | // |
| 87 | // PassManagerImpl is a top level pass manager responsible for managing |
| 88 | // MPPassManagers. |
| 89 | //===----------------------------------------------------------------------===// |
| 90 | |
| 91 | #include "llvm/Support/PrettyStackTrace.h" |
| 92 | |
| 93 | namespace llvm { |
| 94 | class Module; |
| 95 | class Pass; |
| 96 | class StringRef; |
| 97 | class Value; |
| 98 | class Timer; |
| 99 | class PMDataManager; |
| 100 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 101 | // enums for debugging strings |
| 102 | enum PassDebuggingString { |
| 103 | EXECUTION_MSG, // "Executing Pass '" |
| 104 | MODIFICATION_MSG, // "' Made Modification '" |
| 105 | FREEING_MSG, // " Freeing Pass '" |
| 106 | ON_BASICBLOCK_MSG, // "' on BasicBlock '" + PassName + "'...\n" |
| 107 | ON_FUNCTION_MSG, // "' on Function '" + FunctionName + "'...\n" |
| 108 | ON_MODULE_MSG, // "' on Module '" + ModuleName + "'...\n" |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 109 | ON_REGION_MSG, // " 'on Region ...\n'" |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 110 | ON_LOOP_MSG, // " 'on Loop ...\n'" |
| 111 | ON_CG_MSG // "' on Call Graph ...\n'" |
| 112 | }; |
| 113 | |
| 114 | /// PassManagerPrettyStackEntry - This is used to print informative information |
| 115 | /// about what pass is running when/if a stack trace is generated. |
| 116 | class PassManagerPrettyStackEntry : public PrettyStackTraceEntry { |
| 117 | Pass *P; |
| 118 | Value *V; |
| 119 | Module *M; |
| 120 | public: |
| 121 | explicit PassManagerPrettyStackEntry(Pass *p) |
| 122 | : P(p), V(0), M(0) {} // When P is releaseMemory'd. |
| 123 | PassManagerPrettyStackEntry(Pass *p, Value &v) |
| 124 | : P(p), V(&v), M(0) {} // When P is run on V |
| 125 | PassManagerPrettyStackEntry(Pass *p, Module &m) |
| 126 | : P(p), V(0), M(&m) {} // When P is run on M |
| 127 | |
| 128 | /// print - Emit information about this stack frame to OS. |
| 129 | virtual void print(raw_ostream &OS) const; |
| 130 | }; |
| 131 | |
| 132 | |
| 133 | //===----------------------------------------------------------------------===// |
| 134 | // PMStack |
| 135 | // |
| 136 | /// PMStack - This class implements a stack data structure of PMDataManager |
| 137 | /// pointers. |
| 138 | /// |
| 139 | /// Top level pass managers (see PassManager.cpp) maintain active Pass Managers |
| 140 | /// using PMStack. Each Pass implements assignPassManager() to connect itself |
| 141 | /// with appropriate manager. assignPassManager() walks PMStack to find |
| 142 | /// suitable manager. |
| 143 | class PMStack { |
| 144 | public: |
| 145 | typedef std::vector<PMDataManager *>::const_reverse_iterator iterator; |
| 146 | iterator begin() const { return S.rbegin(); } |
| 147 | iterator end() const { return S.rend(); } |
| 148 | |
| 149 | void pop(); |
| 150 | PMDataManager *top() const { return S.back(); } |
| 151 | void push(PMDataManager *PM); |
| 152 | bool empty() const { return S.empty(); } |
| 153 | |
| 154 | void dump() const; |
| 155 | |
| 156 | private: |
| 157 | std::vector<PMDataManager *> S; |
| 158 | }; |
| 159 | |
| 160 | |
| 161 | //===----------------------------------------------------------------------===// |
| 162 | // PMTopLevelManager |
| 163 | // |
| 164 | /// PMTopLevelManager manages LastUser info and collects common APIs used by |
| 165 | /// top level pass managers. |
| 166 | class PMTopLevelManager { |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 167 | protected: |
| 168 | explicit PMTopLevelManager(PMDataManager *PMDM); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 169 | |
| 170 | virtual unsigned getNumContainedManagers() const { |
| 171 | return (unsigned)PassManagers.size(); |
| 172 | } |
| 173 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 174 | void initializeAllAnalysisInfo(); |
| 175 | |
| 176 | private: |
| 177 | /// This is implemented by top level pass manager and used by |
| 178 | /// schedulePass() to add analysis info passes that are not available. |
| 179 | virtual void addTopLevelPass(Pass *P) = 0; |
| 180 | |
| 181 | public: |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 182 | /// Schedule pass P for execution. Make sure that passes required by |
| 183 | /// P are run before P is run. Update analysis info maintained by |
| 184 | /// the manager. Remove dead passes. This is a recursive function. |
| 185 | void schedulePass(Pass *P); |
| 186 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 187 | /// Set pass P as the last user of the given analysis passes. |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 188 | void setLastUser(const SmallVectorImpl<Pass *> &AnalysisPasses, Pass *P); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 189 | |
| 190 | /// Collect passes whose last user is P |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 191 | void collectLastUses(SmallVectorImpl<Pass *> &LastUses, Pass *P); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 192 | |
| 193 | /// Find the pass that implements Analysis AID. Search immutable |
| 194 | /// passes and all pass managers. If desired pass is not found |
| 195 | /// then return NULL. |
| 196 | Pass *findAnalysisPass(AnalysisID AID); |
| 197 | |
| 198 | /// Find analysis usage information for the pass P. |
| 199 | AnalysisUsage *findAnalysisUsage(Pass *P); |
| 200 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 201 | virtual ~PMTopLevelManager(); |
| 202 | |
| 203 | /// Add immutable pass and initialize it. |
| 204 | inline void addImmutablePass(ImmutablePass *P) { |
| 205 | P->initializePass(); |
| 206 | ImmutablePasses.push_back(P); |
| 207 | } |
| 208 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 209 | inline SmallVectorImpl<ImmutablePass *>& getImmutablePasses() { |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 210 | return ImmutablePasses; |
| 211 | } |
| 212 | |
| 213 | void addPassManager(PMDataManager *Manager) { |
| 214 | PassManagers.push_back(Manager); |
| 215 | } |
| 216 | |
| 217 | // Add Manager into the list of managers that are not directly |
| 218 | // maintained by this top level pass manager |
| 219 | inline void addIndirectPassManager(PMDataManager *Manager) { |
| 220 | IndirectPassManagers.push_back(Manager); |
| 221 | } |
| 222 | |
| 223 | // Print passes managed by this top level manager. |
| 224 | void dumpPasses() const; |
| 225 | void dumpArguments() const; |
| 226 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 227 | // Active Pass Managers |
| 228 | PMStack activeStack; |
| 229 | |
| 230 | protected: |
| 231 | |
| 232 | /// Collection of pass managers |
| 233 | SmallVector<PMDataManager *, 8> PassManagers; |
| 234 | |
| 235 | private: |
| 236 | |
| 237 | /// Collection of pass managers that are not directly maintained |
| 238 | /// by this pass manager |
| 239 | SmallVector<PMDataManager *, 8> IndirectPassManagers; |
| 240 | |
| 241 | // Map to keep track of last user of the analysis pass. |
| 242 | // LastUser->second is the last user of Lastuser->first. |
| 243 | DenseMap<Pass *, Pass *> LastUser; |
| 244 | |
| 245 | // Map to keep track of passes that are last used by a pass. |
| 246 | // This inverse map is initialized at PM->run() based on |
| 247 | // LastUser map. |
| 248 | DenseMap<Pass *, SmallPtrSet<Pass *, 8> > InversedLastUser; |
| 249 | |
| 250 | /// Immutable passes are managed by top level manager. |
| 251 | SmallVector<ImmutablePass *, 8> ImmutablePasses; |
| 252 | |
| 253 | DenseMap<Pass *, AnalysisUsage *> AnUsageMap; |
| 254 | }; |
| 255 | |
| 256 | |
| 257 | |
| 258 | //===----------------------------------------------------------------------===// |
| 259 | // PMDataManager |
| 260 | |
| 261 | /// PMDataManager provides the common place to manage the analysis data |
| 262 | /// used by pass managers. |
| 263 | class PMDataManager { |
| 264 | public: |
| 265 | |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 266 | explicit PMDataManager() : TPM(NULL), Depth(0) { |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 267 | initializeAnalysisInfo(); |
| 268 | } |
| 269 | |
| 270 | virtual ~PMDataManager(); |
| 271 | |
| 272 | virtual Pass *getAsPass() = 0; |
| 273 | |
| 274 | /// Augment AvailableAnalysis by adding analysis made available by pass P. |
| 275 | void recordAvailableAnalysis(Pass *P); |
| 276 | |
| 277 | /// verifyPreservedAnalysis -- Verify analysis presreved by pass P. |
| 278 | void verifyPreservedAnalysis(Pass *P); |
| 279 | |
| 280 | /// Remove Analysis that is not preserved by the pass |
| 281 | void removeNotPreservedAnalysis(Pass *P); |
| 282 | |
| 283 | /// Remove dead passes used by P. |
| 284 | void removeDeadPasses(Pass *P, StringRef Msg, |
| 285 | enum PassDebuggingString); |
| 286 | |
| 287 | /// Remove P. |
| 288 | void freePass(Pass *P, StringRef Msg, |
| 289 | enum PassDebuggingString); |
| 290 | |
| 291 | /// Add pass P into the PassVector. Update |
| 292 | /// AvailableAnalysis appropriately if ProcessAnalysis is true. |
| 293 | void add(Pass *P, bool ProcessAnalysis = true); |
| 294 | |
| 295 | /// Add RequiredPass into list of lower level passes required by pass P. |
| 296 | /// RequiredPass is run on the fly by Pass Manager when P requests it |
| 297 | /// through getAnalysis interface. |
| 298 | virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass); |
| 299 | |
| 300 | virtual Pass *getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F); |
| 301 | |
| 302 | /// Initialize available analysis information. |
| 303 | void initializeAnalysisInfo() { |
| 304 | AvailableAnalysis.clear(); |
| 305 | for (unsigned i = 0; i < PMT_Last; ++i) |
| 306 | InheritedAnalysis[i] = NULL; |
| 307 | } |
| 308 | |
| 309 | // Return true if P preserves high level analysis used by other |
| 310 | // passes that are managed by this manager. |
| 311 | bool preserveHigherLevelAnalysis(Pass *P); |
| 312 | |
| 313 | |
| 314 | /// Populate RequiredPasses with analysis pass that are required by |
| 315 | /// pass P and are available. Populate ReqPassNotAvailable with analysis |
| 316 | /// pass that are required by pass P but are not available. |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 317 | void collectRequiredAnalysis(SmallVectorImpl<Pass *> &RequiredPasses, |
| 318 | SmallVectorImpl<AnalysisID> &ReqPassNotAvailable, |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 319 | Pass *P); |
| 320 | |
| 321 | /// All Required analyses should be available to the pass as it runs! Here |
| 322 | /// we fill in the AnalysisImpls member of the pass so that it can |
| 323 | /// successfully use the getAnalysis() method to retrieve the |
| 324 | /// implementations it needs. |
| 325 | void initializeAnalysisImpl(Pass *P); |
| 326 | |
| 327 | /// Find the pass that implements Analysis AID. If desired pass is not found |
| 328 | /// then return NULL. |
| 329 | Pass *findAnalysisPass(AnalysisID AID, bool Direction); |
| 330 | |
| 331 | // Access toplevel manager |
| 332 | PMTopLevelManager *getTopLevelManager() { return TPM; } |
| 333 | void setTopLevelManager(PMTopLevelManager *T) { TPM = T; } |
| 334 | |
| 335 | unsigned getDepth() const { return Depth; } |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 336 | void setDepth(unsigned newDepth) { Depth = newDepth; } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 337 | |
| 338 | // Print routines used by debug-pass |
| 339 | void dumpLastUses(Pass *P, unsigned Offset) const; |
| 340 | void dumpPassArguments() const; |
| 341 | void dumpPassInfo(Pass *P, enum PassDebuggingString S1, |
| 342 | enum PassDebuggingString S2, StringRef Msg); |
| 343 | void dumpRequiredSet(const Pass *P) const; |
| 344 | void dumpPreservedSet(const Pass *P) const; |
| 345 | |
| 346 | virtual unsigned getNumContainedPasses() const { |
| 347 | return (unsigned)PassVector.size(); |
| 348 | } |
| 349 | |
| 350 | virtual PassManagerType getPassManagerType() const { |
| 351 | assert ( 0 && "Invalid use of getPassManagerType"); |
| 352 | return PMT_Unknown; |
| 353 | } |
| 354 | |
| 355 | std::map<AnalysisID, Pass*> *getAvailableAnalysis() { |
| 356 | return &AvailableAnalysis; |
| 357 | } |
| 358 | |
| 359 | // Collect AvailableAnalysis from all the active Pass Managers. |
| 360 | void populateInheritedAnalysis(PMStack &PMS) { |
| 361 | unsigned Index = 0; |
| 362 | for (PMStack::iterator I = PMS.begin(), E = PMS.end(); |
| 363 | I != E; ++I) |
| 364 | InheritedAnalysis[Index++] = (*I)->getAvailableAnalysis(); |
| 365 | } |
| 366 | |
| 367 | protected: |
| 368 | |
| 369 | // Top level manager. |
| 370 | PMTopLevelManager *TPM; |
| 371 | |
| 372 | // Collection of pass that are managed by this manager |
| 373 | SmallVector<Pass *, 16> PassVector; |
| 374 | |
| 375 | // Collection of Analysis provided by Parent pass manager and |
| 376 | // used by current pass manager. At at time there can not be more |
| 377 | // then PMT_Last active pass mangers. |
| 378 | std::map<AnalysisID, Pass *> *InheritedAnalysis[PMT_Last]; |
| 379 | |
| 380 | |
| 381 | /// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions |
| 382 | /// or higher is specified. |
| 383 | bool isPassDebuggingExecutionsOrMore() const; |
| 384 | |
| 385 | private: |
| 386 | void dumpAnalysisUsage(StringRef Msg, const Pass *P, |
| 387 | const AnalysisUsage::VectorType &Set) const; |
| 388 | |
| 389 | // Set of available Analysis. This information is used while scheduling |
| 390 | // pass. If a pass requires an analysis which is not available then |
| 391 | // the required analysis pass is scheduled to run before the pass itself is |
| 392 | // scheduled to run. |
| 393 | std::map<AnalysisID, Pass*> AvailableAnalysis; |
| 394 | |
| 395 | // Collection of higher level analysis used by the pass managed by |
| 396 | // this manager. |
| 397 | SmallVector<Pass *, 8> HigherLevelAnalysis; |
| 398 | |
| 399 | unsigned Depth; |
| 400 | }; |
| 401 | |
| 402 | //===----------------------------------------------------------------------===// |
| 403 | // FPPassManager |
| 404 | // |
| 405 | /// FPPassManager manages BBPassManagers and FunctionPasses. |
| 406 | /// It batches all function passes and basic block pass managers together and |
| 407 | /// sequence them to process one function at a time before processing next |
| 408 | /// function. |
| 409 | class FPPassManager : public ModulePass, public PMDataManager { |
| 410 | public: |
| 411 | static char ID; |
John Bauman | 19bac1e | 2014-05-06 15:23:49 -0400 | [diff] [blame] | 412 | explicit FPPassManager() |
| 413 | : ModulePass(ID), PMDataManager() { } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 414 | |
| 415 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 416 | /// whether any of the passes modifies the module, and if so, return true. |
| 417 | bool runOnFunction(Function &F); |
| 418 | bool runOnModule(Module &M); |
| 419 | |
| 420 | /// cleanup - After running all passes, clean up pass manager cache. |
| 421 | void cleanup(); |
| 422 | |
| 423 | /// doInitialization - Run all of the initializers for the function passes. |
| 424 | /// |
| 425 | bool doInitialization(Module &M); |
| 426 | |
| 427 | /// doFinalization - Run all of the finalizers for the function passes. |
| 428 | /// |
| 429 | bool doFinalization(Module &M); |
| 430 | |
| 431 | virtual PMDataManager *getAsPMDataManager() { return this; } |
| 432 | virtual Pass *getAsPass() { return this; } |
| 433 | |
| 434 | /// Pass Manager itself does not invalidate any analysis info. |
| 435 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 436 | Info.setPreservesAll(); |
| 437 | } |
| 438 | |
| 439 | // Print passes managed by this manager |
| 440 | void dumpPassStructure(unsigned Offset); |
| 441 | |
| 442 | virtual const char *getPassName() const { |
| 443 | return "Function Pass Manager"; |
| 444 | } |
| 445 | |
| 446 | FunctionPass *getContainedPass(unsigned N) { |
| 447 | assert ( N < PassVector.size() && "Pass number out of range!"); |
| 448 | FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]); |
| 449 | return FP; |
| 450 | } |
| 451 | |
| 452 | virtual PassManagerType getPassManagerType() const { |
| 453 | return PMT_FunctionPassManager; |
| 454 | } |
| 455 | }; |
| 456 | |
| 457 | Timer *getPassTimer(Pass *); |
| 458 | |
| 459 | } |
| 460 | |
| 461 | #endif |