LLVM API Documentation

PassNameParser.h

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