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. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "llvm/Pass.h" 17 #include "llvm/IR/Function.h" 18 #include "llvm/IR/IRPrintingPasses.h" 19 #include "llvm/IR/LegacyPassNameParser.h" 20 #include "llvm/IR/Module.h" 21 #include "llvm/IR/OptBisect.h" 22 #include "llvm/PassRegistry.h" 23 #include "llvm/Support/Debug.h" 24 #include "llvm/Support/raw_ostream.h" 25 using namespace llvm; 26 27 #define DEBUG_TYPE "ir" 28 29 //===----------------------------------------------------------------------===// 30 // Pass Implementation 31 // 32 33 // Force out-of-line virtual method. 34 Pass::~Pass() { 35 delete Resolver; 36 } 37 38 // Force out-of-line virtual method. 39 ModulePass::~ModulePass() { } 40 41 Pass *ModulePass::createPrinterPass(raw_ostream &O, 42 const std::string &Banner) const { 43 return createPrintModulePass(O, Banner); 44 } 45 46 PassManagerType ModulePass::getPotentialPassManagerType() const { 47 return PMT_ModulePassManager; 48 } 49 50 bool ModulePass::skipModule(Module &M) const { 51 return !M.getContext().getOptBisect().shouldRunPass(this, M); 52 } 53 54 bool Pass::mustPreserveAnalysisID(char &AID) const { 55 return Resolver->getAnalysisIfAvailable(&AID, true) != nullptr; 56 } 57 58 // dumpPassStructure - Implement the -debug-pass=Structure option 59 void Pass::dumpPassStructure(unsigned Offset) { 60 dbgs().indent(Offset*2) << getPassName() << "\n"; 61 } 62 63 /// getPassName - Return a nice clean name for a pass. This usually 64 /// implemented in terms of the name that is registered by one of the 65 /// Registration templates, but can be overloaded directly. 66 /// 67 StringRef Pass::getPassName() const { 68 AnalysisID AID = getPassID(); 69 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID); 70 if (PI) 71 return PI->getPassName(); 72 return "Unnamed pass: implement Pass::getPassName()"; 73 } 74 75 void Pass::preparePassManager(PMStack &) { 76 // By default, don't do anything. 77 } 78 79 PassManagerType Pass::getPotentialPassManagerType() const { 80 // Default implementation. 81 return PMT_Unknown; 82 } 83 84 void Pass::getAnalysisUsage(AnalysisUsage &) const { 85 // By default, no analysis results are used, all are invalidated. 86 } 87 88 void Pass::releaseMemory() { 89 // By default, don't do anything. 90 } 91 92 void Pass::verifyAnalysis() const { 93 // By default, don't do anything. 94 } 95 96 void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) { 97 return this; 98 } 99 100 ImmutablePass *Pass::getAsImmutablePass() { 101 return nullptr; 102 } 103 104 PMDataManager *Pass::getAsPMDataManager() { 105 return nullptr; 106 } 107 108 void Pass::setResolver(AnalysisResolver *AR) { 109 assert(!Resolver && "Resolver is already set"); 110 Resolver = AR; 111 } 112 113 // print - Print out the internal state of the pass. This is called by Analyze 114 // to print out the contents of an analysis. Otherwise it is not necessary to 115 // implement this method. 116 // 117 void Pass::print(raw_ostream &O,const Module*) const { 118 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n"; 119 } 120 121 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 122 // dump - call print(cerr); 123 LLVM_DUMP_METHOD void Pass::dump() const { 124 print(dbgs(), nullptr); 125 } 126 #endif 127 128 //===----------------------------------------------------------------------===// 129 // ImmutablePass Implementation 130 // 131 // Force out-of-line virtual method. 132 ImmutablePass::~ImmutablePass() { } 133 134 void ImmutablePass::initializePass() { 135 // By default, don't do anything. 136 } 137 138 //===----------------------------------------------------------------------===// 139 // FunctionPass Implementation 140 // 141 142 Pass *FunctionPass::createPrinterPass(raw_ostream &O, 143 const std::string &Banner) const { 144 return createPrintFunctionPass(O, Banner); 145 } 146 147 PassManagerType FunctionPass::getPotentialPassManagerType() const { 148 return PMT_FunctionPassManager; 149 } 150 151 bool FunctionPass::skipFunction(const Function &F) const { 152 if (!F.getContext().getOptBisect().shouldRunPass(this, F)) 153 return true; 154 155 if (F.hasFnAttribute(Attribute::OptimizeNone)) { 156 DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function " 157 << F.getName() << "\n"); 158 return true; 159 } 160 return false; 161 } 162 163 //===----------------------------------------------------------------------===// 164 // BasicBlockPass Implementation 165 // 166 167 Pass *BasicBlockPass::createPrinterPass(raw_ostream &O, 168 const std::string &Banner) const { 169 return createPrintBasicBlockPass(O, Banner); 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::skipBasicBlock(const BasicBlock &BB) const { 183 const Function *F = BB.getParent(); 184 if (!F) 185 return false; 186 if (!F->getContext().getOptBisect().shouldRunPass(this, BB)) 187 return true; 188 if (F->hasFnAttribute(Attribute::OptimizeNone)) { 189 // Report this only once per function. 190 if (&BB == &F->getEntryBlock()) 191 DEBUG(dbgs() << "Skipping pass '" << getPassName() 192 << "' on function " << F->getName() << "\n"); 193 return true; 194 } 195 return false; 196 } 197 198 PassManagerType BasicBlockPass::getPotentialPassManagerType() const { 199 return PMT_BasicBlockPassManager; 200 } 201 202 const PassInfo *Pass::lookupPassInfo(const void *TI) { 203 return PassRegistry::getPassRegistry()->getPassInfo(TI); 204 } 205 206 const PassInfo *Pass::lookupPassInfo(StringRef Arg) { 207 return PassRegistry::getPassRegistry()->getPassInfo(Arg); 208 } 209 210 Pass *Pass::createPass(AnalysisID ID) { 211 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID); 212 if (!PI) 213 return nullptr; 214 return PI->createPass(); 215 } 216 217 //===----------------------------------------------------------------------===// 218 // Analysis Group Implementation Code 219 //===----------------------------------------------------------------------===// 220 221 // RegisterAGBase implementation 222 // 223 RegisterAGBase::RegisterAGBase(StringRef Name, const void *InterfaceID, 224 const void *PassID, bool isDefault) 225 : PassInfo(Name, InterfaceID) { 226 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID, 227 *this, isDefault); 228 } 229 230 //===----------------------------------------------------------------------===// 231 // PassRegistrationListener implementation 232 // 233 234 // enumeratePasses - Iterate over the registered passes, calling the 235 // passEnumerate callback on each PassInfo object. 236 // 237 void PassRegistrationListener::enumeratePasses() { 238 PassRegistry::getPassRegistry()->enumerateWith(this); 239 } 240 241 PassNameParser::PassNameParser(cl::Option &O) 242 : cl::parser<const PassInfo *>(O) { 243 PassRegistry::getPassRegistry()->addRegistrationListener(this); 244 } 245 246 PassNameParser::~PassNameParser() { 247 // This only gets called during static destruction, in which case the 248 // PassRegistry will have already been destroyed by llvm_shutdown(). So 249 // attempting to remove the registration listener is an error. 250 } 251 252 //===----------------------------------------------------------------------===// 253 // AnalysisUsage Class Implementation 254 // 255 256 namespace { 257 struct GetCFGOnlyPasses : public PassRegistrationListener { 258 typedef AnalysisUsage::VectorType VectorType; 259 VectorType &CFGOnlyList; 260 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {} 261 262 void passEnumerate(const PassInfo *P) override { 263 if (P->isCFGOnlyPass()) 264 CFGOnlyList.push_back(P->getTypeInfo()); 265 } 266 }; 267 } 268 269 // setPreservesCFG - This function should be called to by the pass, iff they do 270 // not: 271 // 272 // 1. Add or remove basic blocks from the function 273 // 2. Modify terminator instructions in any way. 274 // 275 // This function annotates the AnalysisUsage info object to say that analyses 276 // that only depend on the CFG are preserved by this pass. 277 // 278 void AnalysisUsage::setPreservesCFG() { 279 // Since this transformation doesn't modify the CFG, it preserves all analyses 280 // that only depend on the CFG (like dominators, loop info, etc...) 281 GetCFGOnlyPasses(Preserved).enumeratePasses(); 282 } 283 284 AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) { 285 const PassInfo *PI = Pass::lookupPassInfo(Arg); 286 // If the pass exists, preserve it. Otherwise silently do nothing. 287 if (PI) Preserved.push_back(PI->getTypeInfo()); 288 return *this; 289 } 290 291 AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) { 292 Required.push_back(ID); 293 return *this; 294 } 295 296 AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) { 297 Required.push_back(&ID); 298 return *this; 299 } 300 301 AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) { 302 Required.push_back(&ID); 303 RequiredTransitive.push_back(&ID); 304 return *this; 305 } 306