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 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 9 | #include "llvm/PassManager.h" |
| 10 | #include "llvm/Module.h" |
| 11 | #include "llvm/Method.h" |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 12 | #include "Support/STLExtras.h" |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 15 | // Source of unique analysis ID #'s. |
| 16 | unsigned AnalysisID::NextID = 0; |
| 17 | |
| 18 | void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) { |
| 19 | assert(P->Resolver == 0 && "Pass already in a PassManager!"); |
| 20 | P->Resolver = AR; |
| 21 | } |
| 22 | |
| 23 | |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 24 | // Pass debugging information. Often it is useful to find out what pass is |
| 25 | // running when a crash occurs in a utility. When this library is compiled with |
| 26 | // debugging on, a command line option (--debug-pass) is enabled that causes the |
| 27 | // pass name to be printed before it executes. |
| 28 | // |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 29 | #ifndef NDEBUG |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 30 | #include "Support/CommandLine.h" |
| 31 | #include <typeinfo> |
| 32 | #include <iostream> |
| 33 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 34 | // Different debug levels that can be enabled... |
| 35 | enum PassDebugLevel { |
| 36 | None, PassStructure, PassExecutions, PassDetails |
| 37 | }; |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 38 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 39 | static cl::Enum<enum PassDebugLevel> PassDebugging("debug-pass", cl::Hidden, |
| 40 | "Print PassManager debugging information", |
| 41 | clEnumVal(None , "disable debug output"), |
| 42 | clEnumVal(PassStructure , "print pass structure before run()"), |
| 43 | clEnumVal(PassExecutions, "print pass name before it is executed"), |
| 44 | clEnumVal(PassDetails , "print pass details when it is executed"), 0); |
| 45 | |
| 46 | void PMDebug::PrintPassStructure(Pass *P) { |
| 47 | if (PassDebugging >= PassStructure) |
| 48 | P->dumpPassStructure(); |
| 49 | } |
| 50 | |
| 51 | void PMDebug::PrintPassInformation(unsigned Depth, const char *Action, |
| 52 | Pass *P, Value *V) { |
| 53 | if (PassDebugging >= PassExecutions) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame^] | 54 | std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '" |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 55 | << typeid(*P).name(); |
| 56 | if (V) { |
| 57 | std::cerr << "' on "; |
| 58 | switch (V->getValueType()) { |
| 59 | case Value::ModuleVal: |
| 60 | std::cerr << "Module\n"; return; |
| 61 | case Value::MethodVal: |
| 62 | std::cerr << "Method '" << V->getName(); break; |
| 63 | case Value::BasicBlockVal: |
| 64 | std::cerr << "BasicBlock '" << V->getName(); break; |
| 65 | default: |
| 66 | std::cerr << typeid(*V).name() << " '" << V->getName(); break; |
| 67 | } |
| 68 | } |
| 69 | std::cerr << "'...\n"; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg, |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame^] | 74 | Pass *P, const Pass::AnalysisSet &Set) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 75 | if (PassDebugging >= PassDetails && !Set.empty()) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame^] | 76 | std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:"; |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 77 | for (unsigned i = 0; i < Set.size(); ++i) { |
| 78 | Pass *P = Set[i].createPass(); // Good thing this is just debug code... |
| 79 | std::cerr << " " << typeid(*P).name(); |
| 80 | delete P; |
| 81 | } |
| 82 | std::cerr << "\n"; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // dumpPassStructure - Implement the -debug-passes=PassStructure option |
| 87 | void Pass::dumpPassStructure(unsigned Offset = 0) { |
| 88 | std::cerr << std::string(Offset*2, ' ') << typeid(*this).name() << "\n"; |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 89 | } |
| 90 | #endif |
| 91 | |
| 92 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 93 | //===----------------------------------------------------------------------===// |
| 94 | // Pass Implementation |
Chris Lattner | 654b5bc | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 95 | // |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 96 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame^] | 97 | void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisSet &Required, |
| 98 | AnalysisSet &Destroyed, AnalysisSet &Provided) { |
| 99 | PM->addPass(this, Required, Destroyed, Provided); |
Chris Lattner | 654b5bc | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 100 | } |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 101 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 102 | //===----------------------------------------------------------------------===// |
| 103 | // MethodPass Implementation |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 104 | // |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 105 | |
| 106 | // run - On a module, we run this pass by initializing, ronOnMethod'ing once |
| 107 | // for every method in the module, then by finalizing. |
| 108 | // |
| 109 | bool MethodPass::run(Module *M) { |
| 110 | bool Changed = doInitialization(M); |
| 111 | |
| 112 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 113 | if (!(*I)->isExternal()) // Passes are not run on external methods! |
| 114 | Changed |= runOnMethod(*I); |
| 115 | |
| 116 | return Changed | doFinalization(M); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 119 | // run - On a method, we simply initialize, run the method, then finalize. |
| 120 | // |
| 121 | bool MethodPass::run(Method *M) { |
| 122 | if (M->isExternal()) return false; // Passes are not run on external methods! |
| 123 | |
| 124 | return doInitialization(M->getParent()) | runOnMethod(M) |
| 125 | | doFinalization(M->getParent()); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 126 | } |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 127 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 128 | void MethodPass::addToPassManager(PassManagerT<Module> *PM, |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame^] | 129 | AnalysisSet &Required, AnalysisSet &Destroyed, |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 130 | AnalysisSet &Provided) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame^] | 131 | PM->addPass(this, Required, Destroyed, Provided); |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 132 | } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 133 | |
| 134 | void MethodPass::addToPassManager(PassManagerT<Method> *PM, |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame^] | 135 | AnalysisSet &Required, AnalysisSet &Destroyed, |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 136 | AnalysisSet &Provided) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame^] | 137 | PM->addPass(this, Required, Destroyed, Provided); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | //===----------------------------------------------------------------------===// |
| 141 | // BasicBlockPass Implementation |
| 142 | // |
| 143 | |
| 144 | // To run this pass on a method, we simply call runOnBasicBlock once for each |
| 145 | // method. |
| 146 | // |
| 147 | bool BasicBlockPass::runOnMethod(Method *M) { |
| 148 | bool Changed = false; |
| 149 | for (Method::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 150 | Changed |= runOnBasicBlock(*I); |
| 151 | return Changed; |
| 152 | } |
| 153 | |
| 154 | // To run directly on the basic block, we initialize, runOnBasicBlock, then |
| 155 | // finalize. |
| 156 | // |
| 157 | bool BasicBlockPass::run(BasicBlock *BB) { |
| 158 | Module *M = BB->getParent()->getParent(); |
| 159 | return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M); |
| 160 | } |
| 161 | |
| 162 | void BasicBlockPass::addToPassManager(PassManagerT<Method> *PM, |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame^] | 163 | AnalysisSet &Required, |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 164 | AnalysisSet &Destroyed, |
| 165 | AnalysisSet &Provided) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame^] | 166 | PM->addPass(this, Required, Destroyed, Provided); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM, |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame^] | 170 | AnalysisSet &Required, |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 171 | AnalysisSet &Destroyed, |
| 172 | AnalysisSet &Provided) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame^] | 173 | PM->addPass(this, Required, Destroyed, Provided); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 174 | } |
| 175 | |