c35f762cb8d4d4fdbeea23da1e843d2ae09de03c
[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->getDataCtor() == 0 &&
52            P->getTargetCtor() == 0) ||
53           ignorablePassImpl(P);
54   }
55
56   // Implement the PassRegistrationListener callbacks used to populate our map
57   //
58   virtual void passRegistered(const PassInfo *P) {
59     if (ignorablePass(P) || !Opt) return;
60     if (findOption(P->getPassArgument()) != getNumOptions()) {
61       std::cerr << "Two passes with the same argument (-"
62                 << P->getPassArgument() << ") attempted to be registered!\n";
63       abort();
64     }
65     addLiteralOption(P->getPassArgument(), P, P->getPassName());
66     Opt->addArgument(P->getPassArgument());
67   }
68   virtual void passEnumerate(const PassInfo *P) { passRegistered(P); }
69
70   virtual void passUnregistered(const PassInfo *P) {
71     if (ignorablePass(P) || !Opt) return;
72     assert(findOption(P->getPassArgument()) != getNumOptions() &&
73            "Registered Pass not in the pass map!");
74     removeLiteralOption(P->getPassArgument());
75     Opt->removeArgument(P->getPassArgument());
76   }
77
78   // ValLessThan - Provide a sorting comparator for Values elements...
79   typedef std::pair<const char*,
80                     std::pair<const PassInfo*, const char*> > ValType;
81   static bool ValLessThan(const ValType &VT1, const ValType &VT2) {
82     return std::string(VT1.first) < std::string(VT2.first);
83   }
84
85   // printOptionInfo - Print out information about this option.  Override the
86   // default implementation to sort the table before we print...
87   virtual void printOptionInfo(const cl::Option &O, unsigned GlobalWidth) const{
88     PassNameParser *PNP = const_cast<PassNameParser*>(this);
89     std::sort(PNP->Values.begin(), PNP->Values.end(), ValLessThan);
90     cl::parser<const PassInfo*>::printOptionInfo(O, GlobalWidth);
91   }
92 };
93
94
95 //===----------------------------------------------------------------------===//
96 // FilteredPassNameParser class - Just like PassNameParser, but filter out
97 // passes that do not have a PassType that includes the flags specified as the
98 // template argument.
99 //
100 template<unsigned Flags>
101 struct FilteredPassNameParser : public PassNameParser {
102
103   // ignorablePassImpl - Can be overriden in subclasses to refine the list of
104   // which passes we want to include.
105   //
106   virtual bool ignorablePassImpl(const PassInfo *P) const {
107     return (P->getPassType() & Flags) == 0;
108   }
109 };
110
111 #endif