Chris Lattner | ab16a65 | 2003-10-13 05:33:01 +0000 | [diff] [blame] | 1 | //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 26e4f89 | 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 | 643b3a0 | 2008-05-23 20:40:06 +0000 | [diff] [blame] | 16 | #include "llvm/Pass.h" |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 17 | #include "llvm/PassManager.h" |
| 18 | #include "llvm/Module.h" |
Misha Brukman | 56a8642 | 2003-10-14 21:38:42 +0000 | [diff] [blame] | 19 | #include "llvm/ModuleProvider.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
Devang Patel | 1155fdf | 2009-10-22 19:36:54 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringMap.h" |
Chris Lattner | 1b368a0 | 2006-12-01 23:27:45 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | 1362602 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Owen Anderson | eae9772 | 2009-06-17 21:16:20 +0000 | [diff] [blame] | 24 | #include "llvm/System/Atomic.h" |
Owen Anderson | ecdab54 | 2009-06-24 00:25:42 +0000 | [diff] [blame] | 25 | #include "llvm/System/Mutex.h" |
Owen Anderson | 7d42b95 | 2009-06-18 16:54:52 +0000 | [diff] [blame] | 26 | #include "llvm/System/Threading.h" |
Jeff Cohen | b622c11 | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 27 | #include <algorithm> |
Dan Gohman | 9988569 | 2008-03-21 23:51:57 +0000 | [diff] [blame] | 28 | #include <map> |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 29 | #include <set> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 33 | // Pass Implementation |
Chris Lattner | 654b5bc | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 34 | // |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 35 | |
Devang Patel | f5a994e | 2006-12-22 22:49:00 +0000 | [diff] [blame] | 36 | // Force out-of-line virtual method. |
Devang Patel | 2c1bba0 | 2007-04-26 21:33:42 +0000 | [diff] [blame] | 37 | Pass::~Pass() { |
| 38 | delete Resolver; |
| 39 | } |
| 40 | |
| 41 | // Force out-of-line virtual method. |
Devang Patel | f5a994e | 2006-12-22 22:49:00 +0000 | [diff] [blame] | 42 | ModulePass::~ModulePass() { } |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 43 | |
Dan Gohman | 1cfbfc8 | 2009-12-14 19:43:09 +0000 | [diff] [blame^] | 44 | PassManagerType ModulePass::getPotentialPassManagerType() const { |
| 45 | return PMT_ModulePassManager; |
| 46 | } |
| 47 | |
Chris Lattner | 9ad7757 | 2003-03-21 21:41:02 +0000 | [diff] [blame] | 48 | bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const { |
Duncan Sands | 5a913d6 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 49 | return Resolver->getAnalysisIfAvailable(AnalysisID, true) != 0; |
Chris Lattner | 9ad7757 | 2003-03-21 21:41:02 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 52 | // dumpPassStructure - Implement the -debug-passes=Structure option |
| 53 | void Pass::dumpPassStructure(unsigned Offset) { |
Chris Lattner | 1362602 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 54 | errs().indent(Offset*2) << getPassName() << "\n"; |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 55 | } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 56 | |
Dan Gohman | 94f57c5 | 2008-03-14 18:27:04 +0000 | [diff] [blame] | 57 | /// getPassName - Return a nice clean name for a pass. This usually |
| 58 | /// implemented in terms of the name that is registered by one of the |
| 59 | /// Registration templates, but can be overloaded directly. |
| 60 | /// |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 61 | const char *Pass::getPassName() const { |
| 62 | if (const PassInfo *PI = getPassInfo()) |
| 63 | return PI->getPassName(); |
Chris Lattner | 9afb8e4 | 2007-10-18 16:11:18 +0000 | [diff] [blame] | 64 | return "Unnamed pass: implement Pass::getPassName()"; |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 65 | } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 66 | |
Dan Gohman | 1cfbfc8 | 2009-12-14 19:43:09 +0000 | [diff] [blame^] | 67 | void Pass::preparePassManager(PMStack &) { |
| 68 | // By default, don't do anything. |
| 69 | } |
| 70 | |
| 71 | PassManagerType Pass::getPotentialPassManagerType() const { |
| 72 | // Default implementation. |
| 73 | return PMT_Unknown; |
| 74 | } |
| 75 | |
| 76 | void Pass::getAnalysisUsage(AnalysisUsage &) const { |
| 77 | // By default, no analysis results are used, all are invalidated. |
| 78 | } |
| 79 | |
| 80 | void Pass::releaseMemory() { |
| 81 | // By default, don't do anything. |
| 82 | } |
| 83 | |
| 84 | void Pass::verifyAnalysis() const { |
| 85 | // By default, don't do anything. |
| 86 | } |
| 87 | |
Misha Brukman | 56a8642 | 2003-10-14 21:38:42 +0000 | [diff] [blame] | 88 | // print - Print out the internal state of the pass. This is called by Analyze |
Misha Brukman | 7eb05a1 | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 89 | // to print out the contents of an analysis. Otherwise it is not necessary to |
Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 90 | // implement this method. |
| 91 | // |
Chris Lattner | 1362602 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 92 | void Pass::print(raw_ostream &O,const Module*) const { |
Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 93 | O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n"; |
| 94 | } |
| 95 | |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 96 | // dump - call print(cerr); |
Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 97 | void Pass::dump() const { |
Chris Lattner | 1362602 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 98 | print(errs(), 0); |
Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 101 | //===----------------------------------------------------------------------===// |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 102 | // ImmutablePass Implementation |
| 103 | // |
Devang Patel | f5a994e | 2006-12-22 22:49:00 +0000 | [diff] [blame] | 104 | // Force out-of-line virtual method. |
| 105 | ImmutablePass::~ImmutablePass() { } |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 106 | |
Dan Gohman | 1cfbfc8 | 2009-12-14 19:43:09 +0000 | [diff] [blame^] | 107 | void ImmutablePass::initializePass() { |
| 108 | // By default, don't do anything. |
| 109 | } |
| 110 | |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 111 | //===----------------------------------------------------------------------===// |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 112 | // FunctionPass Implementation |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 113 | // |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 114 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 115 | // run - On a module, we run this pass by initializing, runOnFunction'ing once |
| 116 | // for every function in the module, then by finalizing. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 117 | // |
Chris Lattner | 79e523d | 2004-09-20 04:47:19 +0000 | [diff] [blame] | 118 | bool FunctionPass::runOnModule(Module &M) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 119 | bool Changed = doInitialization(M); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 121 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 122 | if (!I->isDeclaration()) // Passes are not run on external functions! |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 123 | Changed |= runOnFunction(*I); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 124 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 125 | return Changed | doFinalization(M); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 128 | // run - On a function, we simply initialize, run the function, then finalize. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 129 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 130 | bool FunctionPass::run(Function &F) { |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 131 | // Passes are not run on external functions! |
| 132 | if (F.isDeclaration()) return false; |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 79e523d | 2004-09-20 04:47:19 +0000 | [diff] [blame] | 134 | bool Changed = doInitialization(*F.getParent()); |
| 135 | Changed |= runOnFunction(F); |
| 136 | return Changed | doFinalization(*F.getParent()); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 137 | } |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 138 | |
Dan Gohman | 1cfbfc8 | 2009-12-14 19:43:09 +0000 | [diff] [blame^] | 139 | bool FunctionPass::doInitialization(Module &) { |
| 140 | // By default, don't do anything. |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | bool FunctionPass::doFinalization(Module &) { |
| 145 | // By default, don't do anything. |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | PassManagerType FunctionPass::getPotentialPassManagerType() const { |
| 150 | return PMT_FunctionPassManager; |
| 151 | } |
| 152 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 153 | //===----------------------------------------------------------------------===// |
| 154 | // BasicBlockPass Implementation |
| 155 | // |
| 156 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 157 | // To run this pass on a function, we simply call runOnBasicBlock once for each |
| 158 | // function. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 159 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 160 | bool BasicBlockPass::runOnFunction(Function &F) { |
Chris Lattner | bae3c67 | 2002-09-12 17:06:40 +0000 | [diff] [blame] | 161 | bool Changed = doInitialization(F); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 162 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 163 | Changed |= runOnBasicBlock(*I); |
Chris Lattner | bae3c67 | 2002-09-12 17:06:40 +0000 | [diff] [blame] | 164 | return Changed | doFinalization(F); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Dan Gohman | 1cfbfc8 | 2009-12-14 19:43:09 +0000 | [diff] [blame^] | 167 | bool BasicBlockPass::doInitialization(Module &) { |
| 168 | // By default, don't do anything. |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | bool BasicBlockPass::doInitialization(Function &) { |
| 173 | // By default, don't do anything. |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | bool BasicBlockPass::doFinalization(Function &) { |
| 178 | // By default, don't do anything. |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | bool BasicBlockPass::doFinalization(Module &) { |
| 183 | // By default, don't do anything. |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | PassManagerType BasicBlockPass::getPotentialPassManagerType() const { |
| 188 | return PMT_BasicBlockPassManager; |
| 189 | } |
| 190 | |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 191 | //===----------------------------------------------------------------------===// |
| 192 | // Pass Registration mechanism |
| 193 | // |
Chris Lattner | 4d9fc5e | 2006-12-01 23:46:50 +0000 | [diff] [blame] | 194 | namespace { |
Chris Lattner | 1b368a0 | 2006-12-01 23:27:45 +0000 | [diff] [blame] | 195 | class PassRegistrar { |
Chris Lattner | 4d9fc5e | 2006-12-01 23:46:50 +0000 | [diff] [blame] | 196 | /// PassInfoMap - Keep track of the passinfo object for each registered llvm |
| 197 | /// pass. |
Dan Gohman | 0479aa5 | 2008-05-13 02:05:11 +0000 | [diff] [blame] | 198 | typedef std::map<intptr_t, const PassInfo*> MapType; |
| 199 | MapType PassInfoMap; |
Dan Gohman | 0998427 | 2009-10-08 17:00:02 +0000 | [diff] [blame] | 200 | |
| 201 | typedef StringMap<const PassInfo*> StringMapType; |
| 202 | StringMapType PassInfoStringMap; |
Chris Lattner | 4d9fc5e | 2006-12-01 23:46:50 +0000 | [diff] [blame] | 203 | |
| 204 | /// AnalysisGroupInfo - Keep track of information for each analysis group. |
| 205 | struct AnalysisGroupInfo { |
Chris Lattner | 4d9fc5e | 2006-12-01 23:46:50 +0000 | [diff] [blame] | 206 | std::set<const PassInfo *> Implementations; |
Chris Lattner | 4d9fc5e | 2006-12-01 23:46:50 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | /// AnalysisGroupInfoMap - Information for each analysis group. |
| 210 | std::map<const PassInfo *, AnalysisGroupInfo> AnalysisGroupInfoMap; |
| 211 | |
Chris Lattner | 1b368a0 | 2006-12-01 23:27:45 +0000 | [diff] [blame] | 212 | public: |
| 213 | |
Devang Patel | 0f88f76 | 2007-05-02 20:38:25 +0000 | [diff] [blame] | 214 | const PassInfo *GetPassInfo(intptr_t TI) const { |
Dan Gohman | 0479aa5 | 2008-05-13 02:05:11 +0000 | [diff] [blame] | 215 | MapType::const_iterator I = PassInfoMap.find(TI); |
Chris Lattner | 1b368a0 | 2006-12-01 23:27:45 +0000 | [diff] [blame] | 216 | return I != PassInfoMap.end() ? I->second : 0; |
| 217 | } |
| 218 | |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 219 | const PassInfo *GetPassInfo(StringRef Arg) const { |
Dan Gohman | 0998427 | 2009-10-08 17:00:02 +0000 | [diff] [blame] | 220 | StringMapType::const_iterator I = PassInfoStringMap.find(Arg); |
| 221 | return I != PassInfoStringMap.end() ? I->second : 0; |
| 222 | } |
| 223 | |
Dan Gohman | 0479aa5 | 2008-05-13 02:05:11 +0000 | [diff] [blame] | 224 | void RegisterPass(const PassInfo &PI) { |
Chris Lattner | 1b368a0 | 2006-12-01 23:27:45 +0000 | [diff] [blame] | 225 | bool Inserted = |
Devang Patel | 0f88f76 | 2007-05-02 20:38:25 +0000 | [diff] [blame] | 226 | PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second; |
Chris Lattner | 106b046 | 2008-06-21 19:47:03 +0000 | [diff] [blame] | 227 | assert(Inserted && "Pass registered multiple times!"); Inserted=Inserted; |
Dan Gohman | 0998427 | 2009-10-08 17:00:02 +0000 | [diff] [blame] | 228 | PassInfoStringMap[PI.getPassArgument()] = &PI; |
Chris Lattner | 1b368a0 | 2006-12-01 23:27:45 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Dan Gohman | 0479aa5 | 2008-05-13 02:05:11 +0000 | [diff] [blame] | 231 | void UnregisterPass(const PassInfo &PI) { |
| 232 | MapType::iterator I = PassInfoMap.find(PI.getTypeInfo()); |
Chris Lattner | 1b368a0 | 2006-12-01 23:27:45 +0000 | [diff] [blame] | 233 | assert(I != PassInfoMap.end() && "Pass registered but not in map!"); |
| 234 | |
| 235 | // Remove pass from the map. |
| 236 | PassInfoMap.erase(I); |
Dan Gohman | 0998427 | 2009-10-08 17:00:02 +0000 | [diff] [blame] | 237 | PassInfoStringMap.erase(PI.getPassArgument()); |
Chris Lattner | 1b368a0 | 2006-12-01 23:27:45 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void EnumerateWith(PassRegistrationListener *L) { |
Dan Gohman | 0479aa5 | 2008-05-13 02:05:11 +0000 | [diff] [blame] | 241 | for (MapType::const_iterator I = PassInfoMap.begin(), |
Chris Lattner | 1b368a0 | 2006-12-01 23:27:45 +0000 | [diff] [blame] | 242 | E = PassInfoMap.end(); I != E; ++I) |
| 243 | L->passEnumerate(I->second); |
| 244 | } |
Chris Lattner | 4d9fc5e | 2006-12-01 23:46:50 +0000 | [diff] [blame] | 245 | |
| 246 | |
| 247 | /// Analysis Group Mechanisms. |
| 248 | void RegisterAnalysisGroup(PassInfo *InterfaceInfo, |
| 249 | const PassInfo *ImplementationInfo, |
| 250 | bool isDefault) { |
| 251 | AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo]; |
| 252 | assert(AGI.Implementations.count(ImplementationInfo) == 0 && |
| 253 | "Cannot add a pass to the same analysis group more than once!"); |
| 254 | AGI.Implementations.insert(ImplementationInfo); |
| 255 | if (isDefault) { |
Dan Gohman | 07d0a55 | 2009-08-29 23:34:14 +0000 | [diff] [blame] | 256 | assert(InterfaceInfo->getNormalCtor() == 0 && |
Chris Lattner | 4d9fc5e | 2006-12-01 23:46:50 +0000 | [diff] [blame] | 257 | "Default implementation for analysis group already specified!"); |
| 258 | assert(ImplementationInfo->getNormalCtor() && |
| 259 | "Cannot specify pass as default if it does not have a default ctor"); |
Chris Lattner | 4d9fc5e | 2006-12-01 23:46:50 +0000 | [diff] [blame] | 260 | InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor()); |
| 261 | } |
| 262 | } |
Chris Lattner | 1b368a0 | 2006-12-01 23:27:45 +0000 | [diff] [blame] | 263 | }; |
Chris Lattner | 4d9fc5e | 2006-12-01 23:46:50 +0000 | [diff] [blame] | 264 | } |
Chris Lattner | 1b368a0 | 2006-12-01 23:27:45 +0000 | [diff] [blame] | 265 | |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 266 | static std::vector<PassRegistrationListener*> *Listeners = 0; |
Owen Anderson | ecdab54 | 2009-06-24 00:25:42 +0000 | [diff] [blame] | 267 | static sys::SmartMutex<true> ListenersLock; |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 268 | |
Chris Lattner | a5ebb6a | 2007-04-21 00:12:18 +0000 | [diff] [blame] | 269 | // FIXME: This should use ManagedStatic to manage the pass registrar. |
| 270 | // Unfortunately, we can't do this, because passes are registered with static |
| 271 | // ctors, and having llvm_shutdown clear this map prevents successful |
| 272 | // ressurection after llvm_shutdown is run. |
| 273 | static PassRegistrar *getPassRegistrar() { |
| 274 | static PassRegistrar *PassRegistrarObj = 0; |
Owen Anderson | eae9772 | 2009-06-17 21:16:20 +0000 | [diff] [blame] | 275 | |
| 276 | // Use double-checked locking to safely initialize the registrar when |
| 277 | // we're running in multithreaded mode. |
Owen Anderson | 0fd4eae | 2009-06-18 16:08:27 +0000 | [diff] [blame] | 278 | PassRegistrar* tmp = PassRegistrarObj; |
Owen Anderson | 81241a3 | 2009-06-19 17:45:12 +0000 | [diff] [blame] | 279 | if (llvm_is_multithreaded()) { |
| 280 | sys::MemoryFence(); |
| 281 | if (!tmp) { |
Owen Anderson | eae9772 | 2009-06-17 21:16:20 +0000 | [diff] [blame] | 282 | llvm_acquire_global_lock(); |
Owen Anderson | 0fd4eae | 2009-06-18 16:08:27 +0000 | [diff] [blame] | 283 | tmp = PassRegistrarObj; |
| 284 | if (!tmp) { |
| 285 | tmp = new PassRegistrar(); |
Owen Anderson | eae9772 | 2009-06-17 21:16:20 +0000 | [diff] [blame] | 286 | sys::MemoryFence(); |
| 287 | PassRegistrarObj = tmp; |
| 288 | } |
| 289 | llvm_release_global_lock(); |
Owen Anderson | 0fd4eae | 2009-06-18 16:08:27 +0000 | [diff] [blame] | 290 | } |
Owen Anderson | 81241a3 | 2009-06-19 17:45:12 +0000 | [diff] [blame] | 291 | } else if (!tmp) { |
| 292 | PassRegistrarObj = new PassRegistrar(); |
Nick Lewycky | 87c4a05 | 2009-06-18 03:01:42 +0000 | [diff] [blame] | 293 | } |
Owen Anderson | 81241a3 | 2009-06-19 17:45:12 +0000 | [diff] [blame] | 294 | |
Chris Lattner | a5ebb6a | 2007-04-21 00:12:18 +0000 | [diff] [blame] | 295 | return PassRegistrarObj; |
| 296 | } |
| 297 | |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 298 | // getPassInfo - Return the PassInfo data structure that corresponds to this |
| 299 | // pass... |
| 300 | const PassInfo *Pass::getPassInfo() const { |
Devang Patel | 0f88f76 | 2007-05-02 20:38:25 +0000 | [diff] [blame] | 301 | return lookupPassInfo(PassID); |
Chris Lattner | 4b16963 | 2002-08-21 17:08:37 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Devang Patel | 0f88f76 | 2007-05-02 20:38:25 +0000 | [diff] [blame] | 304 | const PassInfo *Pass::lookupPassInfo(intptr_t TI) { |
Chris Lattner | a5ebb6a | 2007-04-21 00:12:18 +0000 | [diff] [blame] | 305 | return getPassRegistrar()->GetPassInfo(TI); |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 308 | const PassInfo *Pass::lookupPassInfo(StringRef Arg) { |
Dan Gohman | 0998427 | 2009-10-08 17:00:02 +0000 | [diff] [blame] | 309 | return getPassRegistrar()->GetPassInfo(Arg); |
| 310 | } |
| 311 | |
Dan Gohman | 0479aa5 | 2008-05-13 02:05:11 +0000 | [diff] [blame] | 312 | void PassInfo::registerPass() { |
| 313 | getPassRegistrar()->RegisterPass(*this); |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 314 | |
Chris Lattner | 1b368a0 | 2006-12-01 23:27:45 +0000 | [diff] [blame] | 315 | // Notify any listeners. |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 316 | sys::SmartScopedLock<true> Lock(ListenersLock); |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 317 | if (Listeners) |
| 318 | for (std::vector<PassRegistrationListener*>::iterator |
| 319 | I = Listeners->begin(), E = Listeners->end(); I != E; ++I) |
Dan Gohman | 0479aa5 | 2008-05-13 02:05:11 +0000 | [diff] [blame] | 320 | (*I)->passRegistered(this); |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Dan Gohman | 0479aa5 | 2008-05-13 02:05:11 +0000 | [diff] [blame] | 323 | void PassInfo::unregisterPass() { |
| 324 | getPassRegistrar()->UnregisterPass(*this); |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 327 | //===----------------------------------------------------------------------===// |
| 328 | // Analysis Group Implementation Code |
| 329 | //===----------------------------------------------------------------------===// |
| 330 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 331 | // RegisterAGBase implementation |
| 332 | // |
Dan Gohman | 0479aa5 | 2008-05-13 02:05:11 +0000 | [diff] [blame] | 333 | RegisterAGBase::RegisterAGBase(const char *Name, intptr_t InterfaceID, |
Devang Patel | 0f88f76 | 2007-05-02 20:38:25 +0000 | [diff] [blame] | 334 | intptr_t PassID, bool isDefault) |
Dan Gohman | 07d0a55 | 2009-08-29 23:34:14 +0000 | [diff] [blame] | 335 | : PassInfo(Name, InterfaceID) { |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 336 | |
Dan Gohman | 07d0a55 | 2009-08-29 23:34:14 +0000 | [diff] [blame] | 337 | PassInfo *InterfaceInfo = |
| 338 | const_cast<PassInfo*>(Pass::lookupPassInfo(InterfaceID)); |
Chris Lattner | c66da83 | 2006-01-23 01:01:04 +0000 | [diff] [blame] | 339 | if (InterfaceInfo == 0) { |
| 340 | // First reference to Interface, register it now. |
| 341 | registerPass(); |
Dan Gohman | 0479aa5 | 2008-05-13 02:05:11 +0000 | [diff] [blame] | 342 | InterfaceInfo = this; |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 343 | } |
Dan Gohman | 0479aa5 | 2008-05-13 02:05:11 +0000 | [diff] [blame] | 344 | assert(isAnalysisGroup() && |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 345 | "Trying to join an analysis group that is a normal pass!"); |
| 346 | |
Devang Patel | 0f88f76 | 2007-05-02 20:38:25 +0000 | [diff] [blame] | 347 | if (PassID) { |
Dan Gohman | 07d0a55 | 2009-08-29 23:34:14 +0000 | [diff] [blame] | 348 | const PassInfo *ImplementationInfo = Pass::lookupPassInfo(PassID); |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 349 | assert(ImplementationInfo && |
| 350 | "Must register pass before adding to AnalysisGroup!"); |
| 351 | |
Chris Lattner | b3708e2 | 2002-08-30 20:23:45 +0000 | [diff] [blame] | 352 | // Make sure we keep track of the fact that the implementation implements |
| 353 | // the interface. |
| 354 | PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo); |
| 355 | IIPI->addInterfaceImplemented(InterfaceInfo); |
Chris Lattner | 4d9fc5e | 2006-12-01 23:46:50 +0000 | [diff] [blame] | 356 | |
Chris Lattner | a5ebb6a | 2007-04-21 00:12:18 +0000 | [diff] [blame] | 357 | getPassRegistrar()->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault); |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 361 | |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 362 | //===----------------------------------------------------------------------===// |
| 363 | // PassRegistrationListener implementation |
| 364 | // |
| 365 | |
| 366 | // PassRegistrationListener ctor - Add the current object to the list of |
| 367 | // PassRegistrationListeners... |
| 368 | PassRegistrationListener::PassRegistrationListener() { |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 369 | sys::SmartScopedLock<true> Lock(ListenersLock); |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 370 | if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>(); |
| 371 | Listeners->push_back(this); |
| 372 | } |
| 373 | |
| 374 | // dtor - Remove object from list of listeners... |
| 375 | PassRegistrationListener::~PassRegistrationListener() { |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 376 | sys::SmartScopedLock<true> Lock(ListenersLock); |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 377 | std::vector<PassRegistrationListener*>::iterator I = |
| 378 | std::find(Listeners->begin(), Listeners->end(), this); |
| 379 | assert(Listeners && I != Listeners->end() && |
| 380 | "PassRegistrationListener not registered!"); |
| 381 | Listeners->erase(I); |
| 382 | |
| 383 | if (Listeners->empty()) { |
| 384 | delete Listeners; |
| 385 | Listeners = 0; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | // enumeratePasses - Iterate over the registered passes, calling the |
| 390 | // passEnumerate callback on each PassInfo object. |
| 391 | // |
| 392 | void PassRegistrationListener::enumeratePasses() { |
Chris Lattner | a5ebb6a | 2007-04-21 00:12:18 +0000 | [diff] [blame] | 393 | getPassRegistrar()->EnumerateWith(this); |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 394 | } |
Reid Spencer | 8edc8be | 2005-04-25 01:01:35 +0000 | [diff] [blame] | 395 | |
Chris Lattner | 23d5405 | 2006-12-01 22:21:11 +0000 | [diff] [blame] | 396 | //===----------------------------------------------------------------------===// |
| 397 | // AnalysisUsage Class Implementation |
| 398 | // |
| 399 | |
Chris Lattner | 1b368a0 | 2006-12-01 23:27:45 +0000 | [diff] [blame] | 400 | namespace { |
| 401 | struct GetCFGOnlyPasses : public PassRegistrationListener { |
Chris Lattner | cbd160f | 2008-08-08 05:33:04 +0000 | [diff] [blame] | 402 | typedef AnalysisUsage::VectorType VectorType; |
| 403 | VectorType &CFGOnlyList; |
| 404 | GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {} |
Chris Lattner | 1b368a0 | 2006-12-01 23:27:45 +0000 | [diff] [blame] | 405 | |
| 406 | void passEnumerate(const PassInfo *P) { |
| 407 | if (P->isCFGOnlyPass()) |
| 408 | CFGOnlyList.push_back(P); |
| 409 | } |
| 410 | }; |
| 411 | } |
| 412 | |
Chris Lattner | 23d5405 | 2006-12-01 22:21:11 +0000 | [diff] [blame] | 413 | // setPreservesCFG - This function should be called to by the pass, iff they do |
| 414 | // not: |
| 415 | // |
| 416 | // 1. Add or remove basic blocks from the function |
| 417 | // 2. Modify terminator instructions in any way. |
| 418 | // |
| 419 | // This function annotates the AnalysisUsage info object to say that analyses |
| 420 | // that only depend on the CFG are preserved by this pass. |
| 421 | // |
| 422 | void AnalysisUsage::setPreservesCFG() { |
| 423 | // Since this transformation doesn't modify the CFG, it preserves all analyses |
| 424 | // that only depend on the CFG (like dominators, loop info, etc...) |
Chris Lattner | 1b368a0 | 2006-12-01 23:27:45 +0000 | [diff] [blame] | 425 | GetCFGOnlyPasses(Preserved).enumeratePasses(); |
Chris Lattner | 23d5405 | 2006-12-01 22:21:11 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | |