Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 1 | //===- Pass.cpp - LLVM Pass Infrastructure Impementation ------------------===// |
| 2 | // |
| 3 | // This file implements the LLVM Pass infrastructure. It is primarily |
| 4 | // responsible with ensuring that passes are executed and batched together |
| 5 | // optimally. |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/Pass.h" |
| 10 | #include "Support/STLExtras.h" |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame^] | 11 | #include <algorithm> |
| 12 | |
| 13 | // Pass debugging information. Often it is useful to find out what pass is |
| 14 | // running when a crash occurs in a utility. When this library is compiled with |
| 15 | // debugging on, a command line option (--debug-pass) is enabled that causes the |
| 16 | // pass name to be printed before it executes. |
| 17 | // |
| 18 | #ifdef NDEBUG |
| 19 | // If not debugging, remove the option |
| 20 | inline static void PrintPassInformation(const char *, Pass *, Value *) { } |
| 21 | #else |
| 22 | |
| 23 | #include "Support/CommandLine.h" |
| 24 | #include <typeinfo> |
| 25 | #include <iostream> |
| 26 | |
| 27 | // The option is hidden from --help by default |
| 28 | static cl::Flag PassDebugEnabled("debug-pass", |
| 29 | "Print pass names as they are executed by the PassManager", cl::Hidden); |
| 30 | |
| 31 | static void PrintPassInformation(const char *Action, Pass *P, Value *V) { |
| 32 | if (PassDebugEnabled) |
| 33 | std::cerr << Action << " Pass '" << typeid(*P).name() << "' on " |
| 34 | << typeid(*V).name() << " '" << V->getName() << "'...\n"; |
| 35 | } |
| 36 | #endif |
| 37 | |
| 38 | |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 39 | |
| 40 | PassManager::~PassManager() { |
| 41 | for_each(Passes.begin(), Passes.end(), deleter<Pass>); |
| 42 | } |
| 43 | |
| 44 | class BasicBlockPassBatcher : public MethodPass { |
| 45 | typedef std::vector<BasicBlockPass*> SubPassesType; |
| 46 | SubPassesType SubPasses; |
| 47 | public: |
| 48 | ~BasicBlockPassBatcher() { |
| 49 | for_each(SubPasses.begin(), SubPasses.end(), deleter<BasicBlockPass>); |
| 50 | } |
| 51 | |
| 52 | void add(BasicBlockPass *P) { SubPasses.push_back(P); } |
| 53 | |
Chris Lattner | 9c56a28 | 2002-01-22 03:30:25 +0000 | [diff] [blame] | 54 | virtual bool doInitialization(Module *M) { |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 55 | bool Changed = false; |
| 56 | for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end(); |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame^] | 57 | I != E; ++I) { |
| 58 | PrintPassInformation("Initializing", *I, M); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 59 | Changed |= (*I)->doInitialization(M); |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame^] | 60 | } |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 61 | return Changed; |
| 62 | } |
| 63 | |
| 64 | virtual bool runOnMethod(Method *M) { |
| 65 | bool Changed = false; |
| 66 | |
| 67 | for (Method::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) |
| 68 | for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end(); |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame^] | 69 | I != E; ++I) { |
| 70 | PrintPassInformation("Executing", *I, *MI); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 71 | Changed |= (*I)->runOnBasicBlock(*MI); |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame^] | 72 | } |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 73 | return Changed; |
| 74 | } |
| 75 | |
| 76 | virtual bool doFinalization(Module *M) { |
| 77 | bool Changed = false; |
| 78 | for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end(); |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame^] | 79 | I != E; ++I) { |
| 80 | PrintPassInformation("Finalizing", *I, M); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 81 | Changed |= (*I)->doFinalization(M); |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame^] | 82 | } |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 83 | return Changed; |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | class MethodPassBatcher : public Pass { |
| 88 | typedef std::vector<MethodPass*> SubPassesType; |
| 89 | SubPassesType SubPasses; |
| 90 | BasicBlockPassBatcher *BBPBatcher; |
| 91 | public: |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame^] | 92 | inline MethodPassBatcher() : BBPBatcher(0) {} |
| 93 | |
| 94 | inline ~MethodPassBatcher() { |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 95 | for_each(SubPasses.begin(), SubPasses.end(), deleter<MethodPass>); |
| 96 | } |
| 97 | |
Chris Lattner | 654b5bc | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 98 | void add(BasicBlockPass *BBP) { |
| 99 | if (BBPBatcher == 0) { |
| 100 | BBPBatcher = new BasicBlockPassBatcher(); |
| 101 | SubPasses.push_back(BBPBatcher); |
| 102 | } |
| 103 | BBPBatcher->add(BBP); |
| 104 | } |
| 105 | |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 106 | void add(MethodPass *P) { |
| 107 | if (BasicBlockPass *BBP = dynamic_cast<BasicBlockPass*>(P)) { |
Chris Lattner | 654b5bc | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 108 | add(BBP); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 109 | } else { |
| 110 | BBPBatcher = 0; // Ensure that passes don't get accidentally reordered |
| 111 | SubPasses.push_back(P); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | virtual bool run(Module *M) { |
| 116 | bool Changed = false; |
| 117 | for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end(); |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame^] | 118 | I != E; ++I) { |
| 119 | PrintPassInformation("Initializing", *I, M); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 120 | Changed |= (*I)->doInitialization(M); |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame^] | 121 | } |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 122 | |
| 123 | for (Module::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) |
| 124 | for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end(); |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame^] | 125 | I != E; ++I) { |
| 126 | PrintPassInformation("Executing", *I, M); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 127 | Changed |= (*I)->runOnMethod(*MI); |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame^] | 128 | } |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 129 | |
| 130 | for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end(); |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame^] | 131 | I != E; ++I) { |
| 132 | PrintPassInformation("Finalizing", *I, M); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 133 | Changed |= (*I)->doFinalization(M); |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame^] | 134 | } |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 135 | return Changed; |
| 136 | } |
| 137 | }; |
| 138 | |
Chris Lattner | 654b5bc | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 139 | // add(BasicBlockPass*) - If we know it's a BasicBlockPass, we don't have to do |
| 140 | // any checking... |
| 141 | // |
| 142 | void PassManager::add(BasicBlockPass *BBP) { |
| 143 | if (Batcher == 0) // If we don't have a batcher yet, make one now. |
| 144 | add((MethodPass*)BBP); |
| 145 | else |
| 146 | Batcher->add(BBP); |
| 147 | } |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 148 | |
| 149 | |
| 150 | // add(MethodPass*) - MethodPass's must be batched together... make sure this |
| 151 | // happens now. |
| 152 | // |
| 153 | void PassManager::add(MethodPass *MP) { |
| 154 | if (Batcher == 0) { // If we don't have a batcher yet, make one now. |
| 155 | Batcher = new MethodPassBatcher(); |
| 156 | Passes.push_back(Batcher); |
| 157 | } |
| 158 | Batcher->add(MP); // The Batcher will queue them passes up |
| 159 | } |
| 160 | |
| 161 | // add - Add a pass to the PassManager, batching it up as appropriate... |
| 162 | void PassManager::add(Pass *P) { |
| 163 | if (MethodPass *MP = dynamic_cast<MethodPass*>(P)) { |
| 164 | add(MP); // Use the methodpass specific code to do the addition |
| 165 | } else { |
| 166 | Batcher = 0; // Ensure that passes don't get accidentally reordered |
| 167 | Passes.push_back(P); |
| 168 | } |
| 169 | } |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame^] | 170 | |
| 171 | |
| 172 | bool PassManager::run(Module *M) { |
| 173 | bool MadeChanges = false; |
| 174 | // Run all of the pass initializers |
| 175 | for (unsigned i = 0, e = Passes.size(); i < e; ++i) { |
| 176 | PrintPassInformation("Executing", Passes[i], M); |
| 177 | MadeChanges |= Passes[i]->run(M); |
| 178 | } |
| 179 | return MadeChanges; |
| 180 | } |