*** empty log message ***
[oota-llvm.git] / include / llvm / Support / PassNameParser.h
1 //===- llvm/Support/PassNameParser.h ----------------------------*- C++ -*-===//
2 //
3 // This file the PassNameParser and FilteredPassNameParser<> classes, which are
4 // used to add command line arguments to a utility for all of the passes that
5 // have been registered into the system.
6 //
7 // The PassNameParser class adds ALL passes linked into the system (that are
8 // creatable) as command line arguments to the tool (when instantiated with the
9 // appropriate command line option template).  The FilteredPassNameParser<>
10 // template is used for the same purposes as PassNameParser, except that it only
11 // includes passes that have a PassType that are compatible with the filter
12 // (which is the template argument).
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_SUPPORT_PASS_NAME_PARSER_H
17 #define LLVM_SUPPORT_PASS_NAME_PARSER_H
18
19 #include "Support/CommandLine.h"
20 #include "llvm/Pass.h"
21 #include <algorithm>
22
23 //===----------------------------------------------------------------------===//
24 // PassNameParser class - Make use of the pass registration mechanism to
25 // automatically add a command line argument to opt for each pass.
26 //
27 class PassNameParser : public PassRegistrationListener, 
28                        public cl::parser<const PassInfo*> {
29   cl::Option *Opt;
30 public:
31   PassNameParser() : Opt(0) {}
32   
33   void initialize(cl::Option &O) {
34     Opt = &O;
35     cl::parser<const PassInfo*>::initialize(O);
36
37     // Add all of the passes to the map that got initialized before 'this' did.
38     enumeratePasses();
39   }
40
41   // ignorablePassImpl - Can be overriden in subclasses to refine the list of
42   // which passes we want to include.
43   //
44   virtual bool ignorablePassImpl(const PassInfo *P) const { return false; }
45
46   inline bool ignorablePass(const PassInfo *P) const {
47     // Ignore non-selectable and non-constructible passes!  Ignore
48     // non-optimizations.
49     return P->getPassArgument() == 0 ||
50           (P->getNormalCtor() == 0 && P->getDataCtor() == 0 &&
51            P->getTargetCtor() == 0) ||
52           ignorablePassImpl(P);
53   }
54
55   // Implement the PassRegistrationListener callbacks used to populate our map
56   //
57   virtual void passRegistered(const PassInfo *P) {
58     if (ignorablePass(P) || !Opt) return;
59     assert(findOption(P->getPassArgument()) == getNumOptions() &&
60            "Two passes with the same argument attempted to be registered!");
61     addLiteralOption(P->getPassArgument(), P, P->getPassName());
62     Opt->addArgument(P->getPassArgument());
63   }
64   virtual void passEnumerate(const PassInfo *P) { passRegistered(P); }
65
66   virtual void passUnregistered(const PassInfo *P) {
67     if (ignorablePass(P) || !Opt) return;
68     assert(findOption(P->getPassArgument()) != getNumOptions() &&
69            "Registered Pass not in the pass map!");
70     removeLiteralOption(P->getPassArgument());
71     Opt->removeArgument(P->getPassArgument());
72   }
73
74   // ValLessThan - Provide a sorting comparator for Values elements...
75   typedef std::pair<const char*,
76                     std::pair<const PassInfo*, const char*> > ValType;
77   static bool ValLessThan(const ValType &VT1, const ValType &VT2) {
78     return std::string(VT1.first) < std::string(VT2.first);
79   }
80
81   // printOptionInfo - Print out information about this option.  Override the
82   // default implementation to sort the table before we print...
83   virtual void printOptionInfo(const cl::Option &O, unsigned GlobalWidth) const{
84     PassNameParser *PNP = const_cast<PassNameParser*>(this);
85     std::sort(PNP->Values.begin(), PNP->Values.end(), ValLessThan);
86     cl::parser<const PassInfo*>::printOptionInfo(O, GlobalWidth);
87   }
88 };
89
90
91 //===----------------------------------------------------------------------===//
92 // FilteredPassNameParser class - Just like PassNameParser, but filter out
93 // passes that do not have a PassType that includes the flags specified as the
94 // template argument.
95 //
96 template<unsigned Flags>
97 struct FilteredPassNameParser : public PassNameParser {
98
99   // ignorablePassImpl - Can be overriden in subclasses to refine the list of
100   // which passes we want to include.
101   //
102   virtual bool ignorablePassImpl(const PassInfo *P) const {
103     return (P->getPassType() & Flags) == 0;
104   }
105 };
106
107 #endif