c324161f3087c61482b0281395d0a79e218b87e3
[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 #include <iostream>
23
24 //===----------------------------------------------------------------------===//
25 // PassNameParser class - Make use of the pass registration mechanism to
26 // automatically add a command line argument to opt for each pass.
27 //
28 class PassNameParser : public PassRegistrationListener, 
29                        public cl::parser<const PassInfo*> {
30   cl::Option *Opt;
31 public:
32   PassNameParser() : Opt(0) {}
33   
34   void initialize(cl::Option &O) {
35     Opt = &O;
36     cl::parser<const PassInfo*>::initialize(O);
37
38     // Add all of the passes to the map that got initialized before 'this' did.
39     enumeratePasses();
40   }
41
42   // ignorablePassImpl - Can be overriden in subclasses to refine the list of
43   // which passes we want to include.
44   //
45   virtual bool ignorablePassImpl(const PassInfo *P) const { return false; }
46
47   inline bool ignorablePass(const PassInfo *P) const {
48     // Ignore non-selectable and non-constructible passes!  Ignore
49     // non-optimizations.
50     return P->getPassArgument() == 0 || *P->getPassArgument() == 0 ||
51           (P->getNormalCtor() == 0 && 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     if (findOption(P->getPassArgument()) != getNumOptions()) {
60       std::cerr << "Two passes with the same argument (-"
61                 << P->getPassArgument() << ") attempted to be registered!\n";
62       abort();
63     }
64     addLiteralOption(P->getPassArgument(), P, P->getPassName());
65     Opt->addArgument(P->getPassArgument());
66   }
67   virtual void passEnumerate(const PassInfo *P) { passRegistered(P); }
68
69   virtual void passUnregistered(const PassInfo *P) {
70     if (ignorablePass(P) || !Opt) return;
71     assert(findOption(P->getPassArgument()) != getNumOptions() &&
72            "Registered Pass not in the pass map!");
73     removeLiteralOption(P->getPassArgument());
74     Opt->removeArgument(P->getPassArgument());
75   }
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, unsigned 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 //===----------------------------------------------------------------------===//
95 // FilteredPassNameParser class - Just like PassNameParser, but filter out
96 // passes that do not have a PassType that includes the flags specified as the
97 // template argument.
98 //
99 template<unsigned Flags>
100 struct FilteredPassNameParser : public PassNameParser {
101
102   // ignorablePassImpl - Can be overriden in subclasses to refine the list of
103   // which passes we want to include.
104   //
105   virtual bool ignorablePassImpl(const PassInfo *P) const {
106     return (P->getPassType() & Flags) == 0;
107   }
108 };
109
110 #endif