Pass.h revision 697954c15da58bd8b186dbafdedd8b06db770201
1//===- llvm/Pass.h - Base class for XForm Passes -----------------*- C++ -*--=// 2// 3// This file defines a base class that indicates that a specified class is a 4// transformation pass implementation. 5// 6// Pass's are designed this way so that it is possible to apply N passes to a 7// module, by first doing N Pass specific initializations for the module, then 8// looping over all of the methods in the module, doing method specific work 9// N times for each method. Like this: 10// 11// for_each(Passes.begin(), Passes.end(), doPassInitialization(Module)); 12// for_each(Method *M <- Module->begin(), Module->end()) 13// for_each(Passes.begin(), Passes.end(), doPerMethodWork(M)); 14// 15// The other way to do things is like this: 16// for_each(Pass *P <- Passes.begin(), Passes.end()) { 17// Passes->doPassInitialization(Module) 18// for_each(Module->begin(), Module->end(), P->doPerMethodWork); 19// } 20// 21// But this can cause thrashing and poor cache performance, so we don't do it 22// that way. 23// 24// Because a transformation does not see all methods consecutively, it should 25// be careful about the state that it maintains... another pass may modify a 26// method between two invocatations of doPerMethodWork. 27// 28// Also, implementations of doMethodWork should not remove any methods from the 29// module. 30// 31//===----------------------------------------------------------------------===// 32 33#ifndef LLVM_PASS_H 34#define LLVM_PASS_H 35 36#include "llvm/Module.h" 37#include "llvm/Method.h" 38 39//===----------------------------------------------------------------------===// 40// Pass interface - Implemented by all 'passes'. 41// 42struct Pass { 43 //===--------------------------------------------------------------------===// 44 // The externally useful entry points 45 // 46 47 // runAllPasses - Run a bunch of passes on the specified module, efficiently. 48 static bool runAllPasses(Module *M, std::vector<Pass*> &Passes) { 49 bool MadeChanges = false; 50 // Run all of the pass initializers 51 for (unsigned i = 0; i < Passes.size(); ++i) 52 MadeChanges |= Passes[i]->doPassInitialization(M); 53 54 // Loop over all of the methods, applying all of the passes to them 55 for (unsigned m = 0; m < M->size(); ++m) 56 for (unsigned i = 0; i < Passes.size(); ++i) 57 MadeChanges |= Passes[i]->doPerMethodWork(*(M->begin()+m)); 58 59 // Run all of the pass finalizers... 60 for (unsigned i = 0; i < Passes.size(); ++i) 61 MadeChanges |= Passes[i]->doPassFinalization(M); 62 return MadeChanges; 63 } 64 65 // runAllPassesAndFree - Run a bunch of passes on the specified module, 66 // efficiently. When done, delete all of the passes. 67 // 68 static bool runAllPassesAndFree(Module *M, std::vector<Pass*> &Passes) { 69 // First run all of the passes 70 bool MadeChanges = runAllPasses(M, Passes); 71 72 // Free all of the passes. 73 for (unsigned i = 0; i < Passes.size(); ++i) 74 delete Passes[i]; 75 return MadeChanges; 76 } 77 78 79 // run(Module*) - Run this pass on a module and all of the methods contained 80 // within it. Returns true if any of the contained passes returned true. 81 // 82 bool run(Module *M) { 83 bool MadeChanges = doPassInitialization(M); 84 85 // Loop over methods in the module. doPerMethodWork could add a method to 86 // the Module, so we have to keep checking for end of method list condition. 87 // 88 for (unsigned m = 0; m < M->size(); ++m) 89 MadeChanges |= doPerMethodWork(*(M->begin()+m)); 90 return MadeChanges | doPassFinalization(M); 91 } 92 93 // run(Method*) - Run this pass on a module and one specific method. Returns 94 // false on success. 95 // 96 bool run(Method *M) { 97 return doPassInitialization(M->getParent()) | doPerMethodWork(M) | 98 doPassFinalization(M->getParent()); 99 } 100 101 102 //===--------------------------------------------------------------------===// 103 // Functions to be implemented by subclasses 104 // 105 106 // Destructor - Virtual so we can be subclassed 107 inline virtual ~Pass() {} 108 109 // doPassInitialization - Virtual method overridden by subclasses to do 110 // any neccesary per-module initialization. 111 // 112 virtual bool doPassInitialization(Module *M) { return false; } 113 114 // doPerMethodWork - Virtual method overriden by subclasses to do the 115 // per-method processing of the pass. 116 // 117 virtual bool doPerMethodWork(Method *M) { return false; } 118 119 // doPassFinalization - Virtual method overriden by subclasses to do any post 120 // processing needed after all passes have run. 121 // 122 virtual bool doPassFinalization(Module *M) { return false; } 123}; 124 125#endif 126 127