e489e0a6f0b5625fc3c135e4f971307cb5017b68
[oota-llvm.git] / include / llvm / Support / PassNameParser.h
1 //===- llvm/Support/PassNameParser.h ----------------------------*- C++ -*-===//
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 the PassNameParser and FilteredPassNameParser<> classes, which are
11 // used to add command line arguments to a utility for all of the passes that
12 // have been registered into the system.
13 //
14 // The PassNameParser class adds ALL passes linked into the system (that are
15 // creatable) as command line arguments to the tool (when instantiated with the
16 // appropriate command line option template).  The FilteredPassNameParser<>
17 // template is used for the same purposes as PassNameParser, except that it only
18 // includes passes that have a PassType that are compatible with the filter
19 // (which is the template argument).
20 //
21 //===----------------------------------------------------------------------===//
22
23 #ifndef LLVM_SUPPORT_PASS_NAME_PARSER_H
24 #define LLVM_SUPPORT_PASS_NAME_PARSER_H
25
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Pass.h"
28 #include <algorithm>
29 #include <cstring>
30
31 namespace llvm {
32
33 //===----------------------------------------------------------------------===//
34 // PassNameParser class - Make use of the pass registration mechanism to
35 // automatically add a command line argument to opt for each pass.
36 //
37 class PassNameParser : public PassRegistrationListener,
38                        public cl::parser<const PassInfo*> {
39   cl::Option *Opt;
40 public:
41   PassNameParser() : Opt(0) {}
42
43   void initialize(cl::Option &O) {
44     Opt = &O;
45     cl::parser<const PassInfo*>::initialize(O);
46
47     // Add all of the passes to the map that got initialized before 'this' did.
48     enumeratePasses();
49   }
50
51   // ignorablePassImpl - Can be overriden in subclasses to refine the list of
52   // which passes we want to include.
53   //
54   virtual bool ignorablePassImpl(const PassInfo *P) const { return false; }
55
56   inline bool ignorablePass(const PassInfo *P) const {
57     // Ignore non-selectable and non-constructible passes!  Ignore
58     // non-optimizations.
59     return P->getPassArgument() == 0 || *P->getPassArgument() == 0 ||
60            P->getNormalCtor() == 0 || ignorablePassImpl(P);
61   }
62
63   // Implement the PassRegistrationListener callbacks used to populate our map
64   //
65   virtual void passRegistered(const PassInfo *P) {
66     if (ignorablePass(P) || !Opt) return;
67     if (findOption(P->getPassArgument()) != getNumOptions()) {
68       cerr << "Two passes with the same argument (-"
69            << P->getPassArgument() << ") attempted to be registered!\n";
70       abort();
71     }
72     addLiteralOption(P->getPassArgument(), P, P->getPassName());
73   }
74   virtual void passEnumerate(const PassInfo *P) { passRegistered(P); }
75
76   // ValLessThan - Provide a sorting comparator for Values elements...
77   typedef std::pair<const char*,
78                     std::pair<const PassInfo*, const char*> > ValType;
79   static bool ValLessThan(const ValType &VT1, const ValType &VT2) {
80     return std::string(VT1.first) < std::string(VT2.first);
81   }
82
83   // printOptionInfo - Print out information about this option.  Override the
84   // default implementation to sort the table before we print...
85   virtual void printOptionInfo(const cl::Option &O, size_t GlobalWidth) const {
86     PassNameParser *PNP = const_cast<PassNameParser*>(this);
87     std::sort(PNP->Values.begin(), PNP->Values.end(), ValLessThan);
88     cl::parser<const PassInfo*>::printOptionInfo(O, GlobalWidth);
89   }
90 };
91
92 ///===----------------------------------------------------------------------===//
93 /// FilteredPassNameParser class - Make use of the pass registration
94 /// mechanism to automatically add a command line argument to opt for
95 /// each pass that satisfies a filter criteria.  Filter should return
96 /// true for passes to be registered as command-line options.
97 ///
98 template<typename Filter>
99 class FilteredPassNameParser : public PassNameParser {
100 private:
101   Filter filter;
102
103 public:
104   bool ignorablePassImpl(const PassInfo *P) const { return !filter(*P); }
105 };
106
107 ///===----------------------------------------------------------------------===//
108 /// PassArgFilter - A filter for use with PassNameFilterParser that only
109 /// accepts a Pass whose Arg matches certain strings.
110 ///
111 /// Use like this:
112 ///
113 /// extern const char AllowedPassArgs[] = "-anders_aa -dse";
114 ///
115 /// static cl::list<
116 ///   const PassInfo*,
117 ///   bool,
118 ///   FilteredPassNameParser<PassArgFilter<AllowedPassArgs> > >
119 /// PassList(cl::desc("Passes available:"));
120 ///
121 /// Only the -anders_aa and -dse options will be available to the user.
122 ///
123 template<const char *Args>
124 class PassArgFilter {
125 public:
126   bool operator()(const PassInfo &P) const {
127     return(std::strstr(Args, P.getPassArgument()));
128   }
129 };
130
131 } // End llvm namespace
132
133 #endif