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