LLVM  8.0.1
LegacyPassNameParser.h
Go to the documentation of this file.
1 //===- LegacyPassNameParser.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 // Note that this is part of the legacy pass manager infrastructure and will be
22 // (eventually) going away.
23 //
24 //===----------------------------------------------------------------------===//
25 
26 #ifndef LLVM_IR_LEGACYPASSNAMEPARSER_H
27 #define LLVM_IR_LEGACYPASSNAMEPARSER_H
28 
29 #include "llvm/ADT/STLExtras.h"
30 #include "llvm/Pass.h"
34 #include <cstring>
35 
36 namespace llvm {
37 
38 //===----------------------------------------------------------------------===//
39 // PassNameParser class - Make use of the pass registration mechanism to
40 // automatically add a command line argument to opt for each pass.
41 //
43  public cl::parser<const PassInfo*> {
44 public:
46  ~PassNameParser() override;
47 
48  void initialize() {
50 
51  // Add all of the passes to the map that got initialized before 'this' did.
53  }
54 
55  // ignorablePassImpl - Can be overriden in subclasses to refine the list of
56  // which passes we want to include.
57  //
58  virtual bool ignorablePassImpl(const PassInfo *P) const { return false; }
59 
60  inline bool ignorablePass(const PassInfo *P) const {
61  // Ignore non-selectable and non-constructible passes! Ignore
62  // non-optimizations.
63  return P->getPassArgument().empty() || P->getNormalCtor() == nullptr ||
65  }
66 
67  // Implement the PassRegistrationListener callbacks used to populate our map
68  //
69  void passRegistered(const PassInfo *P) override {
70  if (ignorablePass(P)) return;
71  if (findOption(P->getPassArgument().data()) != getNumOptions()) {
72  errs() << "Two passes with the same argument (-"
73  << P->getPassArgument() << ") attempted to be registered!\n";
74  llvm_unreachable(nullptr);
75  }
77  }
78  void passEnumerate(const PassInfo *P) override { passRegistered(P); }
79 
80  // printOptionInfo - Print out information about this option. Override the
81  // default implementation to sort the table before we print...
82  void printOptionInfo(const cl::Option &O, size_t GlobalWidth) const override {
83  PassNameParser *PNP = const_cast<PassNameParser*>(this);
84  array_pod_sort(PNP->Values.begin(), PNP->Values.end(), ValCompare);
86  }
87 
88 private:
89  // ValCompare - Provide a sorting comparator for Values elements...
90  static int ValCompare(const PassNameParser::OptionInfo *VT1,
91  const PassNameParser::OptionInfo *VT2) {
92  return VT1->Name.compare(VT2->Name);
93  }
94 };
95 
96 ///===----------------------------------------------------------------------===//
97 /// FilteredPassNameParser class - Make use of the pass registration
98 /// mechanism to automatically add a command line argument to opt for
99 /// each pass that satisfies a filter criteria. Filter should return
100 /// true for passes to be registered as command-line options.
101 ///
102 template<typename Filter>
104 private:
105  Filter filter;
106 
107 public:
108  bool ignorablePassImpl(const PassInfo *P) const override {
109  return !filter(*P);
110  }
111 };
112 
113 ///===----------------------------------------------------------------------===//
114 /// PassArgFilter - A filter for use with PassNameFilterParser that only
115 /// accepts a Pass whose Arg matches certain strings.
116 ///
117 /// Use like this:
118 ///
119 /// extern const char AllowedPassArgs[] = "-anders_aa -dse";
120 ///
121 /// static cl::list<
122 /// const PassInfo*,
123 /// bool,
124 /// FilteredPassNameParser<PassArgFilter<AllowedPassArgs> > >
125 /// PassList(cl::desc("Passes available:"));
126 ///
127 /// Only the -anders_aa and -dse options will be available to the user.
128 ///
129 template<const char *Args>
131 public:
132  bool operator()(const PassInfo &P) const {
133  return StringRef(Args).contains(P.getPassArgument());
134  }
135 };
136 
137 } // End llvm namespace
138 
139 #endif
SmallVector< OptionInfo, 8 > Values
Definition: CommandLine.h:764
void enumeratePasses()
enumeratePasses - Iterate over the registered passes, calling the passEnumerate callback on each Pass...
Definition: Pass.cpp:243
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
PassNameParser(cl::Option &O)
Definition: Pass.cpp:247
This class represents lattice values for constants.
Definition: AllocatorList.h:24
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition: StringRef.h:448
unsigned getNumOptions() const override
Definition: CommandLine.h:772
bool ignorablePassImpl(const PassInfo *P) const override
===-------------------------------------------------------------------—===// FilteredPassNameParser ...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
~PassNameParser() override
virtual void printOptionInfo(const Option &O, size_t GlobalWidth) const
NormalCtor_t getNormalCtor() const
getNormalCtor - Return a pointer to a function, that when called, creates an instance of the pass and...
Definition: PassInfo.h:89
bool ignorablePass(const PassInfo *P) const
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
#define P(N)
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
Definition: STLExtras.h:1083
bool operator()(const PassInfo &P) const
void passEnumerate(const PassInfo *P) override
passEnumerate - Callback function invoked when someone calls enumeratePasses on this PassRegistration...
PassInfo class - An instance of this class exists for every pass known by the system, and can be obtained from a live Pass by calling its getPassInfo() method.
Definition: PassInfo.h:31
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
===-------------------------------------------------------------------—===// PassArgFilter - A filte...
void addLiteralOption(StringRef Name, const DT &V, StringRef HelpStr)
addLiteralOption - Add an entry to the mapping table.
Definition: CommandLine.h:803
void printOptionInfo(const cl::Option &O, size_t GlobalWidth) const override
StringRef getPassName() const
getPassName - Return the friendly name for the pass, never returns null
Definition: PassInfo.h:63
PassRegistrationListener class - This class is meant to be derived from by clients that are intereste...
Definition: PassSupport.h:197
void passRegistered(const PassInfo *P) override
Callback functions - These functions are invoked whenever a pass is loaded or removed from the curren...
virtual bool ignorablePassImpl(const PassInfo *P) const
StringRef getPassArgument() const
getPassArgument - Return the command line option that may be passed to &#39;opt&#39; that will cause this pas...
Definition: PassInfo.h:68
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
unsigned findOption(StringRef Name)
constexpr char Args[]
Key for Kernel::Metadata::mArgs.