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