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