Chris Lattner | 3e58d9b | 2003-10-13 05:33:01 +0000 | [diff] [blame] | 1 | //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | d695328 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the LLVM Pass infrastructure. It is primarily |
| 11 | // responsible with ensuring that passes are executed and batched together |
| 12 | // optimally. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Dan Gohman | ee335e3 | 2008-05-23 20:40:06 +0000 | [diff] [blame] | 16 | #include "llvm/Pass.h" |
David Greene | 5c8aa95 | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 17 | #include "llvm/Assembly/PrintModulePass.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/PassRegistry.h" |
David Greene | 840d41e | 2010-01-05 01:30:04 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Chris Lattner | 6eeb734 | 2010-01-22 06:29:25 +0000 | [diff] [blame] | 20 | #include "llvm/Support/PassNameParser.h" |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 27ad137 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4130086 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 25 | // Pass Implementation |
Chris Lattner | 35f07eb | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 26 | // |
Chris Lattner | 4130086 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 27 | |
Devang Patel | c7d0f4b | 2006-12-22 22:49:00 +0000 | [diff] [blame] | 28 | // Force out-of-line virtual method. |
Andrew Trick | 8247e0d | 2012-02-03 05:12:30 +0000 | [diff] [blame] | 29 | Pass::~Pass() { |
| 30 | delete Resolver; |
Devang Patel | 6e21ff0 | 2007-04-26 21:33:42 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | // Force out-of-line virtual method. |
Devang Patel | c7d0f4b | 2006-12-22 22:49:00 +0000 | [diff] [blame] | 34 | ModulePass::~ModulePass() { } |
Chris Lattner | d695328 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 35 | |
David Greene | 5c8aa95 | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 36 | Pass *ModulePass::createPrinterPass(raw_ostream &O, |
| 37 | const std::string &Banner) const { |
| 38 | return createPrintModulePass(&O, false, Banner); |
| 39 | } |
| 40 | |
Dan Gohman | eda23fa | 2009-12-14 19:43:09 +0000 | [diff] [blame] | 41 | PassManagerType ModulePass::getPotentialPassManagerType() const { |
| 42 | return PMT_ModulePassManager; |
| 43 | } |
| 44 | |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 45 | bool Pass::mustPreserveAnalysisID(char &AID) const { |
| 46 | return Resolver->getAnalysisIfAvailable(&AID, true) != 0; |
Chris Lattner | f1ab454 | 2003-03-21 21:41:02 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Joe Abbey | 62faf77 | 2011-11-21 04:42:21 +0000 | [diff] [blame] | 49 | // dumpPassStructure - Implement the -debug-pass=Structure option |
Dan Gohman | 8a757ae | 2010-08-19 01:29:07 +0000 | [diff] [blame] | 50 | void Pass::dumpPassStructure(unsigned Offset) { |
David Greene | 840d41e | 2010-01-05 01:30:04 +0000 | [diff] [blame] | 51 | dbgs().indent(Offset*2) << getPassName() << "\n"; |
Chris Lattner | 37d66c4 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 52 | } |
Chris Lattner | 96c466b | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 53 | |
Dan Gohman | b973d5f | 2008-03-14 18:27:04 +0000 | [diff] [blame] | 54 | /// getPassName - Return a nice clean name for a pass. This usually |
| 55 | /// implemented in terms of the name that is registered by one of the |
| 56 | /// Registration templates, but can be overloaded directly. |
| 57 | /// |
Chris Lattner | 44050fb | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 58 | const char *Pass::getPassName() const { |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 59 | AnalysisID AID = getPassID(); |
| 60 | const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID); |
| 61 | if (PI) |
Chris Lattner | 44050fb | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 62 | return PI->getPassName(); |
Chris Lattner | fe3e3f4 | 2007-10-18 16:11:18 +0000 | [diff] [blame] | 63 | return "Unnamed pass: implement Pass::getPassName()"; |
Chris Lattner | 44050fb | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 64 | } |
Chris Lattner | 96c466b | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 65 | |
Dan Gohman | eda23fa | 2009-12-14 19:43:09 +0000 | [diff] [blame] | 66 | void Pass::preparePassManager(PMStack &) { |
| 67 | // By default, don't do anything. |
| 68 | } |
| 69 | |
| 70 | PassManagerType Pass::getPotentialPassManagerType() const { |
| 71 | // Default implementation. |
Andrew Trick | 8247e0d | 2012-02-03 05:12:30 +0000 | [diff] [blame] | 72 | return PMT_Unknown; |
Dan Gohman | eda23fa | 2009-12-14 19:43:09 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void Pass::getAnalysisUsage(AnalysisUsage &) const { |
| 76 | // By default, no analysis results are used, all are invalidated. |
| 77 | } |
| 78 | |
| 79 | void Pass::releaseMemory() { |
| 80 | // By default, don't do anything. |
| 81 | } |
| 82 | |
| 83 | void Pass::verifyAnalysis() const { |
| 84 | // By default, don't do anything. |
| 85 | } |
| 86 | |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 87 | void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) { |
Dan Gohman | e407c1d | 2010-06-21 18:46:45 +0000 | [diff] [blame] | 88 | return this; |
| 89 | } |
| 90 | |
| 91 | ImmutablePass *Pass::getAsImmutablePass() { |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | PMDataManager *Pass::getAsPMDataManager() { |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | void Pass::setResolver(AnalysisResolver *AR) { |
| 100 | assert(!Resolver && "Resolver is already set"); |
| 101 | Resolver = AR; |
| 102 | } |
| 103 | |
Misha Brukman | 69c856a | 2003-10-14 21:38:42 +0000 | [diff] [blame] | 104 | // print - Print out the internal state of the pass. This is called by Analyze |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 105 | // to print out the contents of an analysis. Otherwise it is not necessary to |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 106 | // implement this method. |
| 107 | // |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 108 | void Pass::print(raw_ostream &O,const Module*) const { |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 109 | O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n"; |
| 110 | } |
| 111 | |
Bill Wendling | 832171c | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 112 | // dump - call print(cerr); |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 113 | void Pass::dump() const { |
David Greene | 840d41e | 2010-01-05 01:30:04 +0000 | [diff] [blame] | 114 | print(dbgs(), 0); |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Chris Lattner | 4130086 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 117 | //===----------------------------------------------------------------------===// |
Chris Lattner | 70b4f3e | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 118 | // ImmutablePass Implementation |
| 119 | // |
Devang Patel | c7d0f4b | 2006-12-22 22:49:00 +0000 | [diff] [blame] | 120 | // Force out-of-line virtual method. |
| 121 | ImmutablePass::~ImmutablePass() { } |
Chris Lattner | 70b4f3e | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 122 | |
Dan Gohman | eda23fa | 2009-12-14 19:43:09 +0000 | [diff] [blame] | 123 | void ImmutablePass::initializePass() { |
| 124 | // By default, don't do anything. |
| 125 | } |
| 126 | |
Chris Lattner | 70b4f3e | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 127 | //===----------------------------------------------------------------------===// |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 128 | // FunctionPass Implementation |
Chris Lattner | d695328 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 129 | // |
Chris Lattner | 4130086 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 130 | |
David Greene | 5c8aa95 | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 131 | Pass *FunctionPass::createPrinterPass(raw_ostream &O, |
| 132 | const std::string &Banner) const { |
| 133 | return createPrintFunctionPass(Banner, &O); |
| 134 | } |
| 135 | |
Dan Gohman | eda23fa | 2009-12-14 19:43:09 +0000 | [diff] [blame] | 136 | PassManagerType FunctionPass::getPotentialPassManagerType() const { |
| 137 | return PMT_FunctionPassManager; |
| 138 | } |
| 139 | |
Chris Lattner | 4130086 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 140 | //===----------------------------------------------------------------------===// |
| 141 | // BasicBlockPass Implementation |
| 142 | // |
| 143 | |
David Greene | 5c8aa95 | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 144 | Pass *BasicBlockPass::createPrinterPass(raw_ostream &O, |
| 145 | const std::string &Banner) const { |
Sergei Larin | 68b2faf | 2013-02-08 23:37:41 +0000 | [diff] [blame] | 146 | return createPrintBasicBlockPass(&O, false, Banner); |
David Greene | 5c8aa95 | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Dan Gohman | eda23fa | 2009-12-14 19:43:09 +0000 | [diff] [blame] | 149 | bool BasicBlockPass::doInitialization(Function &) { |
| 150 | // By default, don't do anything. |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | bool BasicBlockPass::doFinalization(Function &) { |
| 155 | // By default, don't do anything. |
| 156 | return false; |
| 157 | } |
| 158 | |
Dan Gohman | eda23fa | 2009-12-14 19:43:09 +0000 | [diff] [blame] | 159 | PassManagerType BasicBlockPass::getPotentialPassManagerType() const { |
Andrew Trick | 8247e0d | 2012-02-03 05:12:30 +0000 | [diff] [blame] | 160 | return PMT_BasicBlockPassManager; |
Dan Gohman | eda23fa | 2009-12-14 19:43:09 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 163 | const PassInfo *Pass::lookupPassInfo(const void *TI) { |
Owen Anderson | aac07ea | 2010-07-20 21:22:24 +0000 | [diff] [blame] | 164 | return PassRegistry::getPassRegistry()->getPassInfo(TI); |
Chris Lattner | 54bbdb4 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Owen Anderson | 8be3291 | 2010-07-20 08:26:15 +0000 | [diff] [blame] | 167 | const PassInfo *Pass::lookupPassInfo(StringRef Arg) { |
Owen Anderson | aac07ea | 2010-07-20 21:22:24 +0000 | [diff] [blame] | 168 | return PassRegistry::getPassRegistry()->getPassInfo(Arg); |
Dan Gohman | 8a261e4 | 2009-10-08 17:00:02 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Andrew Trick | 5e108ee | 2012-02-15 03:21:47 +0000 | [diff] [blame] | 171 | Pass *Pass::createPass(AnalysisID ID) { |
| 172 | const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID); |
Andrew Trick | ebe18ef | 2012-02-08 21:22:34 +0000 | [diff] [blame] | 173 | if (!PI) |
| 174 | return NULL; |
| 175 | return PI->createPass(); |
| 176 | } |
| 177 | |
Owen Anderson | 8be3291 | 2010-07-20 08:26:15 +0000 | [diff] [blame] | 178 | Pass *PassInfo::createPass() const { |
Dan Gohman | e407c1d | 2010-06-21 18:46:45 +0000 | [diff] [blame] | 179 | assert((!isAnalysisGroup() || NormalCtor) && |
| 180 | "No default implementation found for analysis group!"); |
| 181 | assert(NormalCtor && |
| 182 | "Cannot call createPass on PassInfo without default ctor!"); |
| 183 | return NormalCtor(); |
| 184 | } |
| 185 | |
Chris Lattner | 789bc84 | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 186 | //===----------------------------------------------------------------------===// |
| 187 | // Analysis Group Implementation Code |
| 188 | //===----------------------------------------------------------------------===// |
| 189 | |
Chris Lattner | 789bc84 | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 190 | // RegisterAGBase implementation |
| 191 | // |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 192 | RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID, |
| 193 | const void *PassID, bool isDefault) |
Owen Anderson | 9650983 | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 194 | : PassInfo(Name, InterfaceID) { |
| 195 | PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID, |
| 196 | *this, isDefault); |
Chris Lattner | 789bc84 | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Chris Lattner | 54bbdb4 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 199 | //===----------------------------------------------------------------------===// |
| 200 | // PassRegistrationListener implementation |
| 201 | // |
| 202 | |
| 203 | // PassRegistrationListener ctor - Add the current object to the list of |
| 204 | // PassRegistrationListeners... |
| 205 | PassRegistrationListener::PassRegistrationListener() { |
Owen Anderson | 5396735 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 206 | PassRegistry::getPassRegistry()->addRegistrationListener(this); |
Chris Lattner | 54bbdb4 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // dtor - Remove object from list of listeners... |
| 210 | PassRegistrationListener::~PassRegistrationListener() { |
Owen Anderson | 5396735 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 211 | PassRegistry::getPassRegistry()->removeRegistrationListener(this); |
Chris Lattner | 54bbdb4 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | // enumeratePasses - Iterate over the registered passes, calling the |
| 215 | // passEnumerate callback on each PassInfo object. |
| 216 | // |
| 217 | void PassRegistrationListener::enumeratePasses() { |
Owen Anderson | aac07ea | 2010-07-20 21:22:24 +0000 | [diff] [blame] | 218 | PassRegistry::getPassRegistry()->enumerateWith(this); |
Chris Lattner | 54bbdb4 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 219 | } |
Reid Spencer | e8f3848 | 2005-04-25 01:01:35 +0000 | [diff] [blame] | 220 | |
Chris Lattner | 6eeb734 | 2010-01-22 06:29:25 +0000 | [diff] [blame] | 221 | PassNameParser::~PassNameParser() {} |
| 222 | |
Chris Lattner | 947c768 | 2006-12-01 22:21:11 +0000 | [diff] [blame] | 223 | //===----------------------------------------------------------------------===// |
| 224 | // AnalysisUsage Class Implementation |
| 225 | // |
| 226 | |
Chris Lattner | baf2ecd | 2006-12-01 23:27:45 +0000 | [diff] [blame] | 227 | namespace { |
| 228 | struct GetCFGOnlyPasses : public PassRegistrationListener { |
Chris Lattner | fc65d38 | 2008-08-08 05:33:04 +0000 | [diff] [blame] | 229 | typedef AnalysisUsage::VectorType VectorType; |
| 230 | VectorType &CFGOnlyList; |
| 231 | GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {} |
Andrew Trick | 8247e0d | 2012-02-03 05:12:30 +0000 | [diff] [blame] | 232 | |
Owen Anderson | 8be3291 | 2010-07-20 08:26:15 +0000 | [diff] [blame] | 233 | void passEnumerate(const PassInfo *P) { |
Chris Lattner | baf2ecd | 2006-12-01 23:27:45 +0000 | [diff] [blame] | 234 | if (P->isCFGOnlyPass()) |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 235 | CFGOnlyList.push_back(P->getTypeInfo()); |
Chris Lattner | baf2ecd | 2006-12-01 23:27:45 +0000 | [diff] [blame] | 236 | } |
| 237 | }; |
| 238 | } |
| 239 | |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 240 | // setPreservesCFG - This function should be called to by the pass, iff they do |
Chris Lattner | 947c768 | 2006-12-01 22:21:11 +0000 | [diff] [blame] | 241 | // not: |
| 242 | // |
| 243 | // 1. Add or remove basic blocks from the function |
| 244 | // 2. Modify terminator instructions in any way. |
| 245 | // |
| 246 | // This function annotates the AnalysisUsage info object to say that analyses |
| 247 | // that only depend on the CFG are preserved by this pass. |
| 248 | // |
| 249 | void AnalysisUsage::setPreservesCFG() { |
| 250 | // Since this transformation doesn't modify the CFG, it preserves all analyses |
| 251 | // that only depend on the CFG (like dominators, loop info, etc...) |
Chris Lattner | baf2ecd | 2006-12-01 23:27:45 +0000 | [diff] [blame] | 252 | GetCFGOnlyPasses(Preserved).enumeratePasses(); |
Chris Lattner | 947c768 | 2006-12-01 22:21:11 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 255 | AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) { |
| 256 | const PassInfo *PI = Pass::lookupPassInfo(Arg); |
| 257 | // If the pass exists, preserve it. Otherwise silently do nothing. |
| 258 | if (PI) Preserved.push_back(PI->getTypeInfo()); |
| 259 | return *this; |
| 260 | } |
| 261 | |
| 262 | AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) { |
Dan Gohman | e407c1d | 2010-06-21 18:46:45 +0000 | [diff] [blame] | 263 | Required.push_back(ID); |
| 264 | return *this; |
| 265 | } |
Chris Lattner | 947c768 | 2006-12-01 22:21:11 +0000 | [diff] [blame] | 266 | |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 267 | AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) { |
| 268 | Required.push_back(&ID); |
| 269 | return *this; |
| 270 | } |
| 271 | |
| 272 | AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) { |
| 273 | Required.push_back(&ID); |
| 274 | RequiredTransitive.push_back(&ID); |
Dan Gohman | e407c1d | 2010-06-21 18:46:45 +0000 | [diff] [blame] | 275 | return *this; |
| 276 | } |