Changes For Bug 352
[oota-llvm.git] / include / llvm / Support / PassNameParser.h
1 //===- llvm/Support/PassNameParser.h ----------------------------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 <iostream>
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 && P->getTargetCtor() == 0) ||
61           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       std::cerr << "Two passes with the same argument (-"
70                 << P->getPassArgument() << ") attempted to be registered!\n";
71       abort();
72     }
73     addLiteralOption(P->getPassArgument(), P, P->getPassName());
74     Opt->addArgument(P->getPassArgument());
75   }
76   virtual void passEnumerate(const PassInfo *P) { passRegistered(P); }
77
78   virtual void passUnregistered(const PassInfo *P) {
79     if (ignorablePass(P) || !Opt) return;
80     assert(findOption(P->getPassArgument()) != getNumOptions() &&
81            "Registered Pass not in the pass map!");
82     removeLiteralOption(P->getPassArgument());
83     Opt->removeArgument(P->getPassArgument());
84   }
85
86   // ValLessThan - Provide a sorting comparator for Values elements...
87   typedef std::pair<const char*,
88                     std::pair<const PassInfo*, const char*> > ValType;
89   static bool ValLessThan(const ValType &VT1, const ValType &VT2) {
90     return std::string(VT1.first) < std::string(VT2.first);
91   }
92
93   // printOptionInfo - Print out information about this option.  Override the
94   // default implementation to sort the table before we print...
95   virtual void printOptionInfo(const cl::Option &O, unsigned GlobalWidth) const{
96     PassNameParser *PNP = const_cast<PassNameParser*>(this);
97     std::sort(PNP->Values.begin(), PNP->Values.end(), ValLessThan);
98     cl::parser<const PassInfo*>::printOptionInfo(O, GlobalWidth);
99   }
100 };
101
102
103 //===----------------------------------------------------------------------===//
104 // FilteredPassNameParser class - Just like PassNameParser, but filter out
105 // passes that do not have a PassType that includes the flags specified as the
106 // template argument.
107 //
108 template<unsigned Flags>
109 struct FilteredPassNameParser : public PassNameParser {
110
111   // ignorablePassImpl - Can be overriden in subclasses to refine the list of
112   // which passes we want to include.
113   //
114   virtual bool ignorablePassImpl(const PassInfo *P) const {
115     return (P->getPassType() & Flags) == 0;
116   }
117 };
118
119 } // End llvm namespace
120
121 #endif