Added LLVM copyright header (for lack of a better term).
[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 "Support/CommandLine.h"
27 #include "llvm/Pass.h"
28 #include <algorithm>
29 #include <iostream>
30
31 //===----------------------------------------------------------------------===//
32 // PassNameParser class - Make use of the pass registration mechanism to
33 // automatically add a command line argument to opt for each pass.
34 //
35 class PassNameParser : public PassRegistrationListener, 
36                        public cl::parser<const PassInfo*> {
37   cl::Option *Opt;
38 public:
39   PassNameParser() : Opt(0) {}
40   
41   void initialize(cl::Option &O) {
42     Opt = &O;
43     cl::parser<const PassInfo*>::initialize(O);
44
45     // Add all of the passes to the map that got initialized before 'this' did.
46     enumeratePasses();
47   }
48
49   // ignorablePassImpl - Can be overriden in subclasses to refine the list of
50   // which passes we want to include.
51   //
52   virtual bool ignorablePassImpl(const PassInfo *P) const { return false; }
53
54   inline bool ignorablePass(const PassInfo *P) const {
55     // Ignore non-selectable and non-constructible passes!  Ignore
56     // non-optimizations.
57     return P->getPassArgument() == 0 || *P->getPassArgument() == 0 ||
58           (P->getNormalCtor() == 0 && P->getTargetCtor() == 0) ||
59           ignorablePassImpl(P);
60   }
61
62   // Implement the PassRegistrationListener callbacks used to populate our map
63   //
64   virtual void passRegistered(const PassInfo *P) {
65     if (ignorablePass(P) || !Opt) return;
66     if (findOption(P->getPassArgument()) != getNumOptions()) {
67       std::cerr << "Two passes with the same argument (-"
68                 << P->getPassArgument() << ") attempted to be registered!\n";
69       abort();
70     }
71     addLiteralOption(P->getPassArgument(), P, P->getPassName());
72     Opt->addArgument(P->getPassArgument());
73   }
74   virtual void passEnumerate(const PassInfo *P) { passRegistered(P); }
75
76   virtual void passUnregistered(const PassInfo *P) {
77     if (ignorablePass(P) || !Opt) return;
78     assert(findOption(P->getPassArgument()) != getNumOptions() &&
79            "Registered Pass not in the pass map!");
80     removeLiteralOption(P->getPassArgument());
81     Opt->removeArgument(P->getPassArgument());
82   }
83
84   // ValLessThan - Provide a sorting comparator for Values elements...
85   typedef std::pair<const char*,
86                     std::pair<const PassInfo*, const char*> > ValType;
87   static bool ValLessThan(const ValType &VT1, const ValType &VT2) {
88     return std::string(VT1.first) < std::string(VT2.first);
89   }
90
91   // printOptionInfo - Print out information about this option.  Override the
92   // default implementation to sort the table before we print...
93   virtual void printOptionInfo(const cl::Option &O, unsigned GlobalWidth) const{
94     PassNameParser *PNP = const_cast<PassNameParser*>(this);
95     std::sort(PNP->Values.begin(), PNP->Values.end(), ValLessThan);
96     cl::parser<const PassInfo*>::printOptionInfo(O, GlobalWidth);
97   }
98 };
99
100
101 //===----------------------------------------------------------------------===//
102 // FilteredPassNameParser class - Just like PassNameParser, but filter out
103 // passes that do not have a PassType that includes the flags specified as the
104 // template argument.
105 //
106 template<unsigned Flags>
107 struct FilteredPassNameParser : public PassNameParser {
108
109   // ignorablePassImpl - Can be overriden in subclasses to refine the list of
110   // which passes we want to include.
111   //
112   virtual bool ignorablePassImpl(const PassInfo *P) const {
113     return (P->getPassType() & Flags) == 0;
114   }
115 };
116
117 #endif