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