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