1 //===- LegacyPassNameParser.h -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
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).
21 // Note that this is part of the legacy pass manager infrastructure and will be
22 // (eventually) going away.
24 //===----------------------------------------------------------------------===//
26 #ifndef LLVM_IR_LEGACYPASSNAMEPARSER_H
27 #define LLVM_IR_LEGACYPASSNAMEPARSER_H
29 #include "llvm/ADT/STLExtras.h"
30 #include "llvm/Pass.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/raw_ostream.h"
38 //===----------------------------------------------------------------------===//
39 // PassNameParser class - Make use of the pass registration mechanism to
40 // automatically add a command line argument to opt for each pass.
42 class PassNameParser : public PassRegistrationListener,
43 public cl::parser<const PassInfo*> {
47 virtual ~PassNameParser();
49 void initialize(cl::Option &O) {
51 cl::parser<const PassInfo*>::initialize(O);
53 // Add all of the passes to the map that got initialized before 'this' did.
57 // ignorablePassImpl - Can be overriden in subclasses to refine the list of
58 // which passes we want to include.
60 virtual bool ignorablePassImpl(const PassInfo *P) const { return false; }
62 inline bool ignorablePass(const PassInfo *P) const {
63 // Ignore non-selectable and non-constructible passes! Ignore
65 return P->getPassArgument() == nullptr || *P->getPassArgument() == 0 ||
66 P->getNormalCtor() == nullptr || ignorablePassImpl(P);
69 // Implement the PassRegistrationListener callbacks used to populate our map
71 void passRegistered(const PassInfo *P) override {
72 if (ignorablePass(P) || !Opt) return;
73 if (findOption(P->getPassArgument()) != getNumOptions()) {
74 errs() << "Two passes with the same argument (-"
75 << P->getPassArgument() << ") attempted to be registered!\n";
76 llvm_unreachable(nullptr);
78 addLiteralOption(P->getPassArgument(), P, P->getPassName());
80 void passEnumerate(const PassInfo *P) override { passRegistered(P); }
82 // printOptionInfo - Print out information about this option. Override the
83 // default implementation to sort the table before we print...
84 void printOptionInfo(const cl::Option &O, size_t GlobalWidth) const override {
85 PassNameParser *PNP = const_cast<PassNameParser*>(this);
86 array_pod_sort(PNP->Values.begin(), PNP->Values.end(), ValLessThan);
87 cl::parser<const PassInfo*>::printOptionInfo(O, GlobalWidth);
91 // ValLessThan - Provide a sorting comparator for Values elements...
92 static int ValLessThan(const PassNameParser::OptionInfo *VT1,
93 const PassNameParser::OptionInfo *VT2) {
94 return std::strcmp(VT1->Name, VT2->Name);
98 ///===----------------------------------------------------------------------===//
99 /// FilteredPassNameParser class - Make use of the pass registration
100 /// mechanism to automatically add a command line argument to opt for
101 /// each pass that satisfies a filter criteria. Filter should return
102 /// true for passes to be registered as command-line options.
104 template<typename Filter>
105 class FilteredPassNameParser : public PassNameParser {
110 bool ignorablePassImpl(const PassInfo *P) const override {
115 ///===----------------------------------------------------------------------===//
116 /// PassArgFilter - A filter for use with PassNameFilterParser that only
117 /// accepts a Pass whose Arg matches certain strings.
121 /// extern const char AllowedPassArgs[] = "-anders_aa -dse";
126 /// FilteredPassNameParser<PassArgFilter<AllowedPassArgs> > >
127 /// PassList(cl::desc("Passes available:"));
129 /// Only the -anders_aa and -dse options will be available to the user.
131 template<const char *Args>
132 class PassArgFilter {
134 bool operator()(const PassInfo &P) const {
135 return(std::strstr(Args, P.getPassArgument()));
139 } // End llvm namespace