LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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 was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source 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 <iostream>
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 && P->getTargetCtor() == 0) ||
00061           ignorablePassImpl(P);
00062   }
00063 
00064   // Implement the PassRegistrationListener callbacks used to populate our map
00065   //
00066   virtual void passRegistered(const PassInfo *P) {
00067     if (ignorablePass(P) || !Opt) return;
00068     if (findOption(P->getPassArgument()) != getNumOptions()) {
00069       std::cerr << "Two passes with the same argument (-"
00070                 << P->getPassArgument() << ") attempted to be registered!\n";
00071       abort();
00072     }
00073     addLiteralOption(P->getPassArgument(), P, P->getPassName());
00074     Opt->addArgument(P->getPassArgument());
00075   }
00076   virtual void passEnumerate(const PassInfo *P) { passRegistered(P); }
00077 
00078   virtual void passUnregistered(const PassInfo *P) {
00079     if (ignorablePass(P) || !Opt) return;
00080     assert(findOption(P->getPassArgument()) != getNumOptions() &&
00081            "Registered Pass not in the pass map!");
00082     removeLiteralOption(P->getPassArgument());
00083     Opt->removeArgument(P->getPassArgument());
00084   }
00085 
00086   // ValLessThan - Provide a sorting comparator for Values elements...
00087   typedef std::pair<const char*,
00088                     std::pair<const PassInfo*, const char*> > ValType;
00089   static bool ValLessThan(const ValType &VT1, const ValType &VT2) {
00090     return std::string(VT1.first) < std::string(VT2.first);
00091   }
00092 
00093   // printOptionInfo - Print out information about this option.  Override the
00094   // default implementation to sort the table before we print...
00095   virtual void printOptionInfo(const cl::Option &O, unsigned GlobalWidth) const{
00096     PassNameParser *PNP = const_cast<PassNameParser*>(this);
00097     std::sort(PNP->Values.begin(), PNP->Values.end(), ValLessThan);
00098     cl::parser<const PassInfo*>::printOptionInfo(O, GlobalWidth);
00099   }
00100 };
00101 
00102 
00103 //===----------------------------------------------------------------------===//
00104 // FilteredPassNameParser class - Just like PassNameParser, but filter out
00105 // passes that do not have a PassType that includes the flags specified as the
00106 // template argument.
00107 //
00108 template<unsigned Flags>
00109 struct FilteredPassNameParser : public PassNameParser {
00110 
00111   // ignorablePassImpl - Can be overriden in subclasses to refine the list of
00112   // which passes we want to include.
00113   //
00114   virtual bool ignorablePassImpl(const PassInfo *P) const {
00115     return (P->getPassType() & Flags) == 0;
00116   }
00117 };
00118 
00119 } // End llvm namespace
00120 
00121 #endif