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