remove a dead bool.
[llvm/avr.git] / lib / VMCore / Pass.cpp
blob1278074ef5339222b56b3d5e208fbbc8d0a16de3
1 //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Pass.h"
17 #include "llvm/PassManager.h"
18 #include "llvm/Module.h"
19 #include "llvm/ModuleProvider.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/Support/ManagedStatic.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/System/Atomic.h"
24 #include "llvm/System/Mutex.h"
25 #include "llvm/System/Threading.h"
26 #include <algorithm>
27 #include <map>
28 #include <set>
29 using namespace llvm;
31 //===----------------------------------------------------------------------===//
32 // Pass Implementation
35 // Force out-of-line virtual method.
36 Pass::~Pass() {
37 delete Resolver;
40 // Force out-of-line virtual method.
41 ModulePass::~ModulePass() { }
43 bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
44 return Resolver->getAnalysisIfAvailable(AnalysisID, true) != 0;
47 // dumpPassStructure - Implement the -debug-passes=Structure option
48 void Pass::dumpPassStructure(unsigned Offset) {
49 errs().indent(Offset*2) << getPassName() << "\n";
52 /// getPassName - Return a nice clean name for a pass. This usually
53 /// implemented in terms of the name that is registered by one of the
54 /// Registration templates, but can be overloaded directly.
55 ///
56 const char *Pass::getPassName() const {
57 if (const PassInfo *PI = getPassInfo())
58 return PI->getPassName();
59 return "Unnamed pass: implement Pass::getPassName()";
62 // print - Print out the internal state of the pass. This is called by Analyze
63 // to print out the contents of an analysis. Otherwise it is not necessary to
64 // implement this method.
66 void Pass::print(raw_ostream &O,const Module*) const {
67 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
70 // dump - call print(cerr);
71 void Pass::dump() const {
72 print(errs(), 0);
75 //===----------------------------------------------------------------------===//
76 // ImmutablePass Implementation
78 // Force out-of-line virtual method.
79 ImmutablePass::~ImmutablePass() { }
81 //===----------------------------------------------------------------------===//
82 // FunctionPass Implementation
85 // run - On a module, we run this pass by initializing, runOnFunction'ing once
86 // for every function in the module, then by finalizing.
88 bool FunctionPass::runOnModule(Module &M) {
89 bool Changed = doInitialization(M);
91 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
92 if (!I->isDeclaration()) // Passes are not run on external functions!
93 Changed |= runOnFunction(*I);
95 return Changed | doFinalization(M);
98 // run - On a function, we simply initialize, run the function, then finalize.
100 bool FunctionPass::run(Function &F) {
101 // Passes are not run on external functions!
102 if (F.isDeclaration()) return false;
104 bool Changed = doInitialization(*F.getParent());
105 Changed |= runOnFunction(F);
106 return Changed | doFinalization(*F.getParent());
109 //===----------------------------------------------------------------------===//
110 // BasicBlockPass Implementation
113 // To run this pass on a function, we simply call runOnBasicBlock once for each
114 // function.
116 bool BasicBlockPass::runOnFunction(Function &F) {
117 bool Changed = doInitialization(F);
118 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
119 Changed |= runOnBasicBlock(*I);
120 return Changed | doFinalization(F);
123 //===----------------------------------------------------------------------===//
124 // Pass Registration mechanism
126 namespace {
127 class PassRegistrar {
128 /// PassInfoMap - Keep track of the passinfo object for each registered llvm
129 /// pass.
130 typedef std::map<intptr_t, const PassInfo*> MapType;
131 MapType PassInfoMap;
133 /// AnalysisGroupInfo - Keep track of information for each analysis group.
134 struct AnalysisGroupInfo {
135 std::set<const PassInfo *> Implementations;
138 /// AnalysisGroupInfoMap - Information for each analysis group.
139 std::map<const PassInfo *, AnalysisGroupInfo> AnalysisGroupInfoMap;
141 public:
143 const PassInfo *GetPassInfo(intptr_t TI) const {
144 MapType::const_iterator I = PassInfoMap.find(TI);
145 return I != PassInfoMap.end() ? I->second : 0;
148 void RegisterPass(const PassInfo &PI) {
149 bool Inserted =
150 PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
151 assert(Inserted && "Pass registered multiple times!"); Inserted=Inserted;
154 void UnregisterPass(const PassInfo &PI) {
155 MapType::iterator I = PassInfoMap.find(PI.getTypeInfo());
156 assert(I != PassInfoMap.end() && "Pass registered but not in map!");
158 // Remove pass from the map.
159 PassInfoMap.erase(I);
162 void EnumerateWith(PassRegistrationListener *L) {
163 for (MapType::const_iterator I = PassInfoMap.begin(),
164 E = PassInfoMap.end(); I != E; ++I)
165 L->passEnumerate(I->second);
169 /// Analysis Group Mechanisms.
170 void RegisterAnalysisGroup(PassInfo *InterfaceInfo,
171 const PassInfo *ImplementationInfo,
172 bool isDefault) {
173 AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo];
174 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
175 "Cannot add a pass to the same analysis group more than once!");
176 AGI.Implementations.insert(ImplementationInfo);
177 if (isDefault) {
178 assert(InterfaceInfo->getNormalCtor() == 0 &&
179 "Default implementation for analysis group already specified!");
180 assert(ImplementationInfo->getNormalCtor() &&
181 "Cannot specify pass as default if it does not have a default ctor");
182 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
188 static std::vector<PassRegistrationListener*> *Listeners = 0;
189 static sys::SmartMutex<true> ListenersLock;
191 // FIXME: This should use ManagedStatic to manage the pass registrar.
192 // Unfortunately, we can't do this, because passes are registered with static
193 // ctors, and having llvm_shutdown clear this map prevents successful
194 // ressurection after llvm_shutdown is run.
195 static PassRegistrar *getPassRegistrar() {
196 static PassRegistrar *PassRegistrarObj = 0;
198 // Use double-checked locking to safely initialize the registrar when
199 // we're running in multithreaded mode.
200 PassRegistrar* tmp = PassRegistrarObj;
201 if (llvm_is_multithreaded()) {
202 sys::MemoryFence();
203 if (!tmp) {
204 llvm_acquire_global_lock();
205 tmp = PassRegistrarObj;
206 if (!tmp) {
207 tmp = new PassRegistrar();
208 sys::MemoryFence();
209 PassRegistrarObj = tmp;
211 llvm_release_global_lock();
213 } else if (!tmp) {
214 PassRegistrarObj = new PassRegistrar();
217 return PassRegistrarObj;
220 // getPassInfo - Return the PassInfo data structure that corresponds to this
221 // pass...
222 const PassInfo *Pass::getPassInfo() const {
223 return lookupPassInfo(PassID);
226 const PassInfo *Pass::lookupPassInfo(intptr_t TI) {
227 return getPassRegistrar()->GetPassInfo(TI);
230 void PassInfo::registerPass() {
231 getPassRegistrar()->RegisterPass(*this);
233 // Notify any listeners.
234 sys::SmartScopedLock<true> Lock(ListenersLock);
235 if (Listeners)
236 for (std::vector<PassRegistrationListener*>::iterator
237 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
238 (*I)->passRegistered(this);
241 void PassInfo::unregisterPass() {
242 getPassRegistrar()->UnregisterPass(*this);
245 //===----------------------------------------------------------------------===//
246 // Analysis Group Implementation Code
247 //===----------------------------------------------------------------------===//
249 // RegisterAGBase implementation
251 RegisterAGBase::RegisterAGBase(const char *Name, intptr_t InterfaceID,
252 intptr_t PassID, bool isDefault)
253 : PassInfo(Name, InterfaceID) {
255 PassInfo *InterfaceInfo =
256 const_cast<PassInfo*>(Pass::lookupPassInfo(InterfaceID));
257 if (InterfaceInfo == 0) {
258 // First reference to Interface, register it now.
259 registerPass();
260 InterfaceInfo = this;
262 assert(isAnalysisGroup() &&
263 "Trying to join an analysis group that is a normal pass!");
265 if (PassID) {
266 const PassInfo *ImplementationInfo = Pass::lookupPassInfo(PassID);
267 assert(ImplementationInfo &&
268 "Must register pass before adding to AnalysisGroup!");
270 // Make sure we keep track of the fact that the implementation implements
271 // the interface.
272 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
273 IIPI->addInterfaceImplemented(InterfaceInfo);
275 getPassRegistrar()->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault);
280 //===----------------------------------------------------------------------===//
281 // PassRegistrationListener implementation
284 // PassRegistrationListener ctor - Add the current object to the list of
285 // PassRegistrationListeners...
286 PassRegistrationListener::PassRegistrationListener() {
287 sys::SmartScopedLock<true> Lock(ListenersLock);
288 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
289 Listeners->push_back(this);
292 // dtor - Remove object from list of listeners...
293 PassRegistrationListener::~PassRegistrationListener() {
294 sys::SmartScopedLock<true> Lock(ListenersLock);
295 std::vector<PassRegistrationListener*>::iterator I =
296 std::find(Listeners->begin(), Listeners->end(), this);
297 assert(Listeners && I != Listeners->end() &&
298 "PassRegistrationListener not registered!");
299 Listeners->erase(I);
301 if (Listeners->empty()) {
302 delete Listeners;
303 Listeners = 0;
307 // enumeratePasses - Iterate over the registered passes, calling the
308 // passEnumerate callback on each PassInfo object.
310 void PassRegistrationListener::enumeratePasses() {
311 getPassRegistrar()->EnumerateWith(this);
314 //===----------------------------------------------------------------------===//
315 // AnalysisUsage Class Implementation
318 namespace {
319 struct GetCFGOnlyPasses : public PassRegistrationListener {
320 typedef AnalysisUsage::VectorType VectorType;
321 VectorType &CFGOnlyList;
322 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
324 void passEnumerate(const PassInfo *P) {
325 if (P->isCFGOnlyPass())
326 CFGOnlyList.push_back(P);
331 // setPreservesCFG - This function should be called to by the pass, iff they do
332 // not:
334 // 1. Add or remove basic blocks from the function
335 // 2. Modify terminator instructions in any way.
337 // This function annotates the AnalysisUsage info object to say that analyses
338 // that only depend on the CFG are preserved by this pass.
340 void AnalysisUsage::setPreservesCFG() {
341 // Since this transformation doesn't modify the CFG, it preserves all analyses
342 // that only depend on the CFG (like dominators, loop info, etc...)
343 GetCFGOnlyPasses(Preserved).enumeratePasses();