LLVM API Documentation

LegacyPassNameParser.h
Go to the documentation of this file.
00001 //===- LegacyPassNameParser.h -----------------------------------*- C++ -*-===//
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 contains the PassNameParser and FilteredPassNameParser<> classes,
00011 // which are used to add command line arguments to a utility for all of the
00012 // passes that have been registered into the system.
00013 //
00014 // The PassNameParser class adds ALL passes linked into the system (that are
00015 // creatable) as command line arguments to the tool (when instantiated with the
00016 // appropriate command line option template).  The FilteredPassNameParser<>
00017 // template is used for the same purposes as PassNameParser, except that it only
00018 // includes passes that have a PassType that are compatible with the filter
00019 // (which is the template argument).
00020 //
00021 // Note that this is part of the legacy pass manager infrastructure and will be
00022 // (eventually) going away.
00023 //
00024 //===----------------------------------------------------------------------===//
00025 
00026 #ifndef LLVM_IR_LEGACYPASSNAMEPARSER_H
00027 #define LLVM_IR_LEGACYPASSNAMEPARSER_H
00028 
00029 #include "llvm/ADT/STLExtras.h"
00030 #include "llvm/Pass.h"
00031 #include "llvm/Support/CommandLine.h"
00032 #include "llvm/Support/ErrorHandling.h"
00033 #include "llvm/Support/raw_ostream.h"
00034 #include <cstring>
00035 
00036 namespace llvm {
00037 
00038 //===----------------------------------------------------------------------===//
00039 // PassNameParser class - Make use of the pass registration mechanism to
00040 // automatically add a command line argument to opt for each pass.
00041 //
00042 class PassNameParser : public PassRegistrationListener,
00043                        public cl::parser<const PassInfo*> {
00044   cl::Option *Opt;
00045 public:
00046   PassNameParser();
00047   virtual ~PassNameParser();
00048 
00049   void initialize(cl::Option &O) {
00050     Opt = &O;
00051     cl::parser<const PassInfo*>::initialize(O);
00052 
00053     // Add all of the passes to the map that got initialized before 'this' did.
00054     enumeratePasses();
00055   }
00056 
00057   // ignorablePassImpl - Can be overriden in subclasses to refine the list of
00058   // which passes we want to include.
00059   //
00060   virtual bool ignorablePassImpl(const PassInfo *P) const { return false; }
00061 
00062   inline bool ignorablePass(const PassInfo *P) const {
00063     // Ignore non-selectable and non-constructible passes!  Ignore
00064     // non-optimizations.
00065     return P->getPassArgument() == nullptr || *P->getPassArgument() == 0 ||
00066            P->getNormalCtor() == nullptr || ignorablePassImpl(P);
00067   }
00068 
00069   // Implement the PassRegistrationListener callbacks used to populate our map
00070   //
00071   void passRegistered(const PassInfo *P) override {
00072     if (ignorablePass(P) || !Opt) return;
00073     if (findOption(P->getPassArgument()) != getNumOptions()) {
00074       errs() << "Two passes with the same argument (-"
00075            << P->getPassArgument() << ") attempted to be registered!\n";
00076       llvm_unreachable(nullptr);
00077     }
00078     addLiteralOption(P->getPassArgument(), P, P->getPassName());
00079   }
00080   void passEnumerate(const PassInfo *P) override { passRegistered(P); }
00081 
00082   // printOptionInfo - Print out information about this option.  Override the
00083   // default implementation to sort the table before we print...
00084   void printOptionInfo(const cl::Option &O, size_t GlobalWidth) const override {
00085     PassNameParser *PNP = const_cast<PassNameParser*>(this);
00086     array_pod_sort(PNP->Values.begin(), PNP->Values.end(), ValLessThan);
00087     cl::parser<const PassInfo*>::printOptionInfo(O, GlobalWidth);
00088   }
00089 
00090 private:
00091   // ValLessThan - Provide a sorting comparator for Values elements...
00092   static int ValLessThan(const PassNameParser::OptionInfo *VT1,
00093                          const PassNameParser::OptionInfo *VT2) {
00094     return std::strcmp(VT1->Name, VT2->Name);
00095   }
00096 };
00097 
00098 ///===----------------------------------------------------------------------===//
00099 /// FilteredPassNameParser class - Make use of the pass registration
00100 /// mechanism to automatically add a command line argument to opt for
00101 /// each pass that satisfies a filter criteria.  Filter should return
00102 /// true for passes to be registered as command-line options.
00103 ///
00104 template<typename Filter>
00105 class FilteredPassNameParser : public PassNameParser {
00106 private:
00107   Filter filter;
00108 
00109 public:
00110   bool ignorablePassImpl(const PassInfo *P) const override {
00111     return !filter(*P);
00112   }
00113 };
00114 
00115 ///===----------------------------------------------------------------------===//
00116 /// PassArgFilter - A filter for use with PassNameFilterParser that only
00117 /// accepts a Pass whose Arg matches certain strings.
00118 ///
00119 /// Use like this:
00120 ///
00121 /// extern const char AllowedPassArgs[] = "-anders_aa -dse";
00122 ///
00123 /// static cl::list<
00124 ///   const PassInfo*,
00125 ///   bool,
00126 ///   FilteredPassNameParser<PassArgFilter<AllowedPassArgs> > >
00127 /// PassList(cl::desc("Passes available:"));
00128 ///
00129 /// Only the -anders_aa and -dse options will be available to the user.
00130 ///
00131 template<const char *Args>
00132 class PassArgFilter {
00133 public:
00134   bool operator()(const PassInfo &P) const {
00135     return(std::strstr(Args, P.getPassArgument()));
00136   }
00137 };
00138 
00139 } // End llvm namespace
00140 
00141 #endif