LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PassNameParser.h
Go to the documentation of this file.
1 //===- llvm/Support/PassNameParser.h ----------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the PassNameParser and FilteredPassNameParser<> classes,
11 // which are used to add command line arguments to a utility for all of the
12 // passes that 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_PASSNAMEPARSER_H
24 #define LLVM_SUPPORT_PASSNAMEPARSER_H
25 
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/Pass.h"
31 #include <cstring>
32 
33 namespace llvm {
34 
35 //===----------------------------------------------------------------------===//
36 // PassNameParser class - Make use of the pass registration mechanism to
37 // automatically add a command line argument to opt for each pass.
38 //
40  public cl::parser<const PassInfo*> {
41  cl::Option *Opt;
42 public:
43  PassNameParser() : Opt(0) {}
44  virtual ~PassNameParser();
45 
47  Opt = &O;
49 
50  // Add all of the passes to the map that got initialized before 'this' did.
52  }
53 
54  // ignorablePassImpl - Can be overriden in subclasses to refine the list of
55  // which passes we want to include.
56  //
57  virtual bool ignorablePassImpl(const PassInfo *P) const { return false; }
58 
59  inline bool ignorablePass(const PassInfo *P) const {
60  // Ignore non-selectable and non-constructible passes! Ignore
61  // non-optimizations.
62  return P->getPassArgument() == 0 || *P->getPassArgument() == 0 ||
63  P->getNormalCtor() == 0 || ignorablePassImpl(P);
64  }
65 
66  // Implement the PassRegistrationListener callbacks used to populate our map
67  //
68  virtual void passRegistered(const PassInfo *P) {
69  if (ignorablePass(P) || !Opt) return;
70  if (findOption(P->getPassArgument()) != getNumOptions()) {
71  errs() << "Two passes with the same argument (-"
72  << P->getPassArgument() << ") attempted to be registered!\n";
74  }
76  }
77  virtual void passEnumerate(const PassInfo *P) { passRegistered(P); }
78 
79  // printOptionInfo - Print out information about this option. Override the
80  // default implementation to sort the table before we print...
81  virtual void printOptionInfo(const cl::Option &O, size_t GlobalWidth) const {
82  PassNameParser *PNP = const_cast<PassNameParser*>(this);
83  array_pod_sort(PNP->Values.begin(), PNP->Values.end(), ValLessThan);
85  }
86 
87 private:
88  // ValLessThan - Provide a sorting comparator for Values elements...
89  static int ValLessThan(const PassNameParser::OptionInfo *VT1,
90  const PassNameParser::OptionInfo *VT2) {
91  return std::strcmp(VT1->Name, VT2->Name);
92  }
93 };
94 
95 ///===----------------------------------------------------------------------===//
96 /// FilteredPassNameParser class - Make use of the pass registration
97 /// mechanism to automatically add a command line argument to opt for
98 /// each pass that satisfies a filter criteria. Filter should return
99 /// true for passes to be registered as command-line options.
100 ///
101 template<typename Filter>
103 private:
104  Filter filter;
105 
106 public:
107  bool ignorablePassImpl(const PassInfo *P) const { return !filter(*P); }
108 };
109 
110 ///===----------------------------------------------------------------------===//
111 /// PassArgFilter - A filter for use with PassNameFilterParser that only
112 /// accepts a Pass whose Arg matches certain strings.
113 ///
114 /// Use like this:
115 ///
116 /// extern const char AllowedPassArgs[] = "-anders_aa -dse";
117 ///
118 /// static cl::list<
119 /// const PassInfo*,
120 /// bool,
121 /// FilteredPassNameParser<PassArgFilter<AllowedPassArgs> > >
122 /// PassList(cl::desc("Passes available:"));
123 ///
124 /// Only the -anders_aa and -dse options will be available to the user.
125 ///
126 template<const char *Args>
128 public:
129  bool operator()(const PassInfo &P) const {
130  return(std::strstr(Args, P.getPassArgument()));
131  }
132 };
133 
134 } // End llvm namespace
135 
136 #endif
int strcmp(const char *s1, const char *s2);
SmallVector< OptionInfo, 8 > Values
Definition: CommandLine.h:638
raw_ostream & errs()
const char * getPassName() const
Definition: PassSupport.h:72
unsigned findOption(const char *Name)
virtual void passEnumerate(const PassInfo *P)
virtual void printOptionInfo(const Option &O, size_t GlobalWidth) const
#define llvm_unreachable(msg)
virtual void passRegistered(const PassInfo *P)
virtual bool ignorablePassImpl(const PassInfo *P) const
bool ignorablePass(const PassInfo *P) const
void initialize(cl::Option &O)
bool ignorablePassImpl(const PassInfo *P) const
bool operator()(const PassInfo &P) const
NormalCtor_t getNormalCtor() const
Definition: PassSupport.h:103
#define P(N)
void array_pod_sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:289
char *strstr(const char *s1, const char *s2);
virtual ~PassNameParser()
Definition: Pass.cpp:221
const char * getPassArgument() const
Definition: PassSupport.h:78
virtual void printOptionInfo(const cl::Option &O, size_t GlobalWidth) const
void addLiteralOption(const char *Name, const DT &V, const char *HelpStr)
Definition: CommandLine.h:674