LLVM API Documentation

Pass.cpp
Go to the documentation of this file.
00001 //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the LLVM Pass infrastructure.  It is primarily
00011 // responsible with ensuring that passes are executed and batched together
00012 // optimally.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #include "llvm/Pass.h"
00017 #include "llvm/Assembly/PrintModulePass.h"
00018 #include "llvm/PassRegistry.h"
00019 #include "llvm/Support/Debug.h"
00020 #include "llvm/Support/PassNameParser.h"
00021 #include "llvm/Support/raw_ostream.h"
00022 using namespace llvm;
00023 
00024 //===----------------------------------------------------------------------===//
00025 // Pass Implementation
00026 //
00027 
00028 // Force out-of-line virtual method.
00029 Pass::~Pass() {
00030   delete Resolver;
00031 }
00032 
00033 // Force out-of-line virtual method.
00034 ModulePass::~ModulePass() { }
00035 
00036 Pass *ModulePass::createPrinterPass(raw_ostream &O,
00037                                     const std::string &Banner) const {
00038   return createPrintModulePass(&O, false, Banner);
00039 }
00040 
00041 PassManagerType ModulePass::getPotentialPassManagerType() const {
00042   return PMT_ModulePassManager;
00043 }
00044 
00045 bool Pass::mustPreserveAnalysisID(char &AID) const {
00046   return Resolver->getAnalysisIfAvailable(&AID, true) != 0;
00047 }
00048 
00049 // dumpPassStructure - Implement the -debug-pass=Structure option
00050 void Pass::dumpPassStructure(unsigned Offset) {
00051   dbgs().indent(Offset*2) << getPassName() << "\n";
00052 }
00053 
00054 /// getPassName - Return a nice clean name for a pass.  This usually
00055 /// implemented in terms of the name that is registered by one of the
00056 /// Registration templates, but can be overloaded directly.
00057 ///
00058 const char *Pass::getPassName() const {
00059   AnalysisID AID =  getPassID();
00060   const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
00061   if (PI)
00062     return PI->getPassName();
00063   return "Unnamed pass: implement Pass::getPassName()";
00064 }
00065 
00066 void Pass::preparePassManager(PMStack &) {
00067   // By default, don't do anything.
00068 }
00069 
00070 PassManagerType Pass::getPotentialPassManagerType() const {
00071   // Default implementation.
00072   return PMT_Unknown;
00073 }
00074 
00075 void Pass::getAnalysisUsage(AnalysisUsage &) const {
00076   // By default, no analysis results are used, all are invalidated.
00077 }
00078 
00079 void Pass::releaseMemory() {
00080   // By default, don't do anything.
00081 }
00082 
00083 void Pass::verifyAnalysis() const {
00084   // By default, don't do anything.
00085 }
00086 
00087 void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
00088   return this;
00089 }
00090 
00091 ImmutablePass *Pass::getAsImmutablePass() {
00092   return 0;
00093 }
00094 
00095 PMDataManager *Pass::getAsPMDataManager() {
00096   return 0;
00097 }
00098 
00099 void Pass::setResolver(AnalysisResolver *AR) {
00100   assert(!Resolver && "Resolver is already set");
00101   Resolver = AR;
00102 }
00103 
00104 // print - Print out the internal state of the pass.  This is called by Analyze
00105 // to print out the contents of an analysis.  Otherwise it is not necessary to
00106 // implement this method.
00107 //
00108 void Pass::print(raw_ostream &O,const Module*) const {
00109   O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
00110 }
00111 
00112 // dump - call print(cerr);
00113 void Pass::dump() const {
00114   print(dbgs(), 0);
00115 }
00116 
00117 //===----------------------------------------------------------------------===//
00118 // ImmutablePass Implementation
00119 //
00120 // Force out-of-line virtual method.
00121 ImmutablePass::~ImmutablePass() { }
00122 
00123 void ImmutablePass::initializePass() {
00124   // By default, don't do anything.
00125 }
00126 
00127 //===----------------------------------------------------------------------===//
00128 // FunctionPass Implementation
00129 //
00130 
00131 Pass *FunctionPass::createPrinterPass(raw_ostream &O,
00132                                       const std::string &Banner) const {
00133   return createPrintFunctionPass(Banner, &O);
00134 }
00135 
00136 PassManagerType FunctionPass::getPotentialPassManagerType() const {
00137   return PMT_FunctionPassManager;
00138 }
00139 
00140 //===----------------------------------------------------------------------===//
00141 // BasicBlockPass Implementation
00142 //
00143 
00144 Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
00145                                         const std::string &Banner) const {
00146   return createPrintBasicBlockPass(&O, false, Banner);
00147 }
00148 
00149 bool BasicBlockPass::doInitialization(Function &) {
00150   // By default, don't do anything.
00151   return false;
00152 }
00153 
00154 bool BasicBlockPass::doFinalization(Function &) {
00155   // By default, don't do anything.
00156   return false;
00157 }
00158 
00159 PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
00160   return PMT_BasicBlockPassManager;
00161 }
00162 
00163 const PassInfo *Pass::lookupPassInfo(const void *TI) {
00164   return PassRegistry::getPassRegistry()->getPassInfo(TI);
00165 }
00166 
00167 const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
00168   return PassRegistry::getPassRegistry()->getPassInfo(Arg);
00169 }
00170 
00171 Pass *Pass::createPass(AnalysisID ID) {
00172   const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
00173   if (!PI)
00174     return NULL;
00175   return PI->createPass();
00176 }
00177 
00178 Pass *PassInfo::createPass() const {
00179   assert((!isAnalysisGroup() || NormalCtor) &&
00180          "No default implementation found for analysis group!");
00181   assert(NormalCtor &&
00182          "Cannot call createPass on PassInfo without default ctor!");
00183   return NormalCtor();
00184 }
00185 
00186 //===----------------------------------------------------------------------===//
00187 //                  Analysis Group Implementation Code
00188 //===----------------------------------------------------------------------===//
00189 
00190 // RegisterAGBase implementation
00191 //
00192 RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
00193                                const void *PassID, bool isDefault)
00194     : PassInfo(Name, InterfaceID) {
00195   PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
00196                                                          *this, isDefault);
00197 }
00198 
00199 //===----------------------------------------------------------------------===//
00200 // PassRegistrationListener implementation
00201 //
00202 
00203 // PassRegistrationListener ctor - Add the current object to the list of
00204 // PassRegistrationListeners...
00205 PassRegistrationListener::PassRegistrationListener() {
00206   PassRegistry::getPassRegistry()->addRegistrationListener(this);
00207 }
00208 
00209 // dtor - Remove object from list of listeners...
00210 PassRegistrationListener::~PassRegistrationListener() {
00211   PassRegistry::getPassRegistry()->removeRegistrationListener(this);
00212 }
00213 
00214 // enumeratePasses - Iterate over the registered passes, calling the
00215 // passEnumerate callback on each PassInfo object.
00216 //
00217 void PassRegistrationListener::enumeratePasses() {
00218   PassRegistry::getPassRegistry()->enumerateWith(this);
00219 }
00220 
00221 PassNameParser::~PassNameParser() {}
00222 
00223 //===----------------------------------------------------------------------===//
00224 //   AnalysisUsage Class Implementation
00225 //
00226 
00227 namespace {
00228   struct GetCFGOnlyPasses : public PassRegistrationListener {
00229     typedef AnalysisUsage::VectorType VectorType;
00230     VectorType &CFGOnlyList;
00231     GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
00232 
00233     void passEnumerate(const PassInfo *P) {
00234       if (P->isCFGOnlyPass())
00235         CFGOnlyList.push_back(P->getTypeInfo());
00236     }
00237   };
00238 }
00239 
00240 // setPreservesCFG - This function should be called to by the pass, iff they do
00241 // not:
00242 //
00243 //  1. Add or remove basic blocks from the function
00244 //  2. Modify terminator instructions in any way.
00245 //
00246 // This function annotates the AnalysisUsage info object to say that analyses
00247 // that only depend on the CFG are preserved by this pass.
00248 //
00249 void AnalysisUsage::setPreservesCFG() {
00250   // Since this transformation doesn't modify the CFG, it preserves all analyses
00251   // that only depend on the CFG (like dominators, loop info, etc...)
00252   GetCFGOnlyPasses(Preserved).enumeratePasses();
00253 }
00254 
00255 AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
00256   const PassInfo *PI = Pass::lookupPassInfo(Arg);
00257   // If the pass exists, preserve it. Otherwise silently do nothing.
00258   if (PI) Preserved.push_back(PI->getTypeInfo());
00259   return *this;
00260 }
00261 
00262 AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
00263   Required.push_back(ID);
00264   return *this;
00265 }
00266 
00267 AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
00268   Required.push_back(&ID);
00269   return *this;
00270 }
00271 
00272 AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
00273   Required.push_back(&ID);
00274   RequiredTransitive.push_back(&ID);
00275   return *this;
00276 }