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