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" |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame^] | 10 | #include "PassManagerT.h" // PassManagerT implementation |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 11 | #include "llvm/Module.h" |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 12 | #include "llvm/Function.h" |
Chris Lattner | 60a6591 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 13 | #include "llvm/BasicBlock.h" |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 14 | #include "Support/STLExtras.h" |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame^] | 15 | #include "Support/CommandLine.h" |
| 16 | #include <typeinfo> |
| 17 | #include <iostream> |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 18 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 19 | // Source of unique analysis ID #'s. |
| 20 | unsigned AnalysisID::NextID = 0; |
| 21 | |
| 22 | void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) { |
| 23 | assert(P->Resolver == 0 && "Pass already in a PassManager!"); |
| 24 | P->Resolver = AR; |
| 25 | } |
| 26 | |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame^] | 27 | //===----------------------------------------------------------------------===// |
| 28 | // PassManager implementation - The PassManager class is a simple Pimpl class |
| 29 | // that wraps the PassManagerT template. |
| 30 | // |
| 31 | PassManager::PassManager() : PM(new PassManagerT<Module>()) {} |
| 32 | PassManager::~PassManager() { delete PM; } |
| 33 | void PassManager::add(Pass *P) { PM->add(P); } |
| 34 | bool PassManager::run(Module *M) { return PM->run(M); } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame^] | 36 | |
| 37 | //===----------------------------------------------------------------------===// |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 38 | // Pass debugging information. Often it is useful to find out what pass is |
| 39 | // running when a crash occurs in a utility. When this library is compiled with |
| 40 | // debugging on, a command line option (--debug-pass) is enabled that causes the |
| 41 | // pass name to be printed before it executes. |
| 42 | // |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 43 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 44 | // Different debug levels that can be enabled... |
| 45 | enum PassDebugLevel { |
| 46 | None, PassStructure, PassExecutions, PassDetails |
| 47 | }; |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 48 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 49 | static cl::Enum<enum PassDebugLevel> PassDebugging("debug-pass", cl::Hidden, |
| 50 | "Print PassManager debugging information", |
| 51 | clEnumVal(None , "disable debug output"), |
| 52 | clEnumVal(PassStructure , "print pass structure before run()"), |
| 53 | clEnumVal(PassExecutions, "print pass name before it is executed"), |
| 54 | clEnumVal(PassDetails , "print pass details when it is executed"), 0); |
| 55 | |
| 56 | void PMDebug::PrintPassStructure(Pass *P) { |
| 57 | if (PassDebugging >= PassStructure) |
| 58 | P->dumpPassStructure(); |
| 59 | } |
| 60 | |
| 61 | void PMDebug::PrintPassInformation(unsigned Depth, const char *Action, |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 62 | Pass *P, Annotable *V) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 63 | if (PassDebugging >= PassExecutions) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 64 | std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '" |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 65 | << typeid(*P).name(); |
| 66 | if (V) { |
| 67 | std::cerr << "' on "; |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 68 | |
| 69 | if (dynamic_cast<Module*>(V)) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 70 | std::cerr << "Module\n"; return; |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 71 | } else if (Function *F = dynamic_cast<Function*>(V)) |
| 72 | std::cerr << "Function '" << F->getName(); |
| 73 | else if (BasicBlock *BB = dynamic_cast<BasicBlock*>(V)) |
| 74 | std::cerr << "BasicBlock '" << BB->getName(); |
| 75 | else if (Value *Val = dynamic_cast<Value*>(V)) |
| 76 | std::cerr << typeid(*Val).name() << " '" << Val->getName(); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 77 | } |
| 78 | std::cerr << "'...\n"; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 83 | Pass *P, const std::vector<AnalysisID> &Set){ |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 84 | if (PassDebugging >= PassDetails && !Set.empty()) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 85 | std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:"; |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 86 | for (unsigned i = 0; i != Set.size(); ++i) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 87 | Pass *P = Set[i].createPass(); // Good thing this is just debug code... |
| 88 | std::cerr << " " << typeid(*P).name(); |
| 89 | delete P; |
| 90 | } |
| 91 | std::cerr << "\n"; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // dumpPassStructure - Implement the -debug-passes=PassStructure option |
| 96 | void Pass::dumpPassStructure(unsigned Offset = 0) { |
| 97 | std::cerr << std::string(Offset*2, ' ') << typeid(*this).name() << "\n"; |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 98 | } |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 99 | |
| 100 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 101 | //===----------------------------------------------------------------------===// |
| 102 | // Pass Implementation |
Chris Lattner | 654b5bc | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 103 | // |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 104 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 105 | void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) { |
| 106 | PM->addPass(this, AU); |
Chris Lattner | 654b5bc | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 107 | } |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 108 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 109 | //===----------------------------------------------------------------------===// |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 110 | // FunctionPass Implementation |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 111 | // |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 112 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 113 | // run - On a module, we run this pass by initializing, runOnFunction'ing once |
| 114 | // for every function in the module, then by finalizing. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 115 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 116 | bool FunctionPass::run(Module *M) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 117 | bool Changed = doInitialization(M); |
| 118 | |
| 119 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 120 | if (!(*I)->isExternal()) // Passes are not run on external functions! |
| 121 | Changed |= runOnFunction(*I); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 122 | |
| 123 | return Changed | doFinalization(M); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 126 | // run - On a function, we simply initialize, run the function, then finalize. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 127 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 128 | bool FunctionPass::run(Function *F) { |
| 129 | if (F->isExternal()) return false;// Passes are not run on external functions! |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 130 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 131 | return doInitialization(F->getParent()) | runOnFunction(F) |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 132 | | doFinalization(F->getParent()); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 133 | } |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 134 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 135 | void FunctionPass::addToPassManager(PassManagerT<Module> *PM, |
| 136 | AnalysisUsage &AU) { |
| 137 | PM->addPass(this, AU); |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 138 | } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 139 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 140 | void FunctionPass::addToPassManager(PassManagerT<Function> *PM, |
| 141 | AnalysisUsage &AU) { |
| 142 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame^] | 145 | // doesNotModifyCFG - This function should be called by our subclasses to |
| 146 | // implement the getAnalysisUsage virtual function, iff they do not: |
| 147 | // |
| 148 | // 1. Add or remove basic blocks from the function |
| 149 | // 2. Modify terminator instructions in any way. |
| 150 | // |
| 151 | // This function annotates the AnalysisUsage info object to say that analyses |
| 152 | // that only depend on the CFG are preserved by this pass. |
| 153 | // |
| 154 | void FunctionPass::doesNotModifyCFG(AnalysisUsage &Info) { |
| 155 | |
| 156 | } |
| 157 | |
| 158 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 159 | //===----------------------------------------------------------------------===// |
| 160 | // BasicBlockPass Implementation |
| 161 | // |
| 162 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 163 | // To run this pass on a function, we simply call runOnBasicBlock once for each |
| 164 | // function. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 165 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 166 | bool BasicBlockPass::runOnFunction(Function *F) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 167 | bool Changed = false; |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 168 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 169 | Changed |= runOnBasicBlock(*I); |
| 170 | return Changed; |
| 171 | } |
| 172 | |
| 173 | // To run directly on the basic block, we initialize, runOnBasicBlock, then |
| 174 | // finalize. |
| 175 | // |
| 176 | bool BasicBlockPass::run(BasicBlock *BB) { |
| 177 | Module *M = BB->getParent()->getParent(); |
| 178 | return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M); |
| 179 | } |
| 180 | |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 181 | void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 182 | AnalysisUsage &AU) { |
| 183 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 187 | AnalysisUsage &AU) { |
| 188 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 189 | } |
| 190 | |