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