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*> {
45 PassNameParser(cl::Option &O);
46 virtual ~PassNameParser();
49 cl::parser<const PassInfo*>::initialize();
51 // Add all of the passes to the map that got initialized before 'this' did.
55 // ignorablePassImpl - Can be overriden in subclasses to refine the list of
56 // which passes we want to include.
58 virtual bool ignorablePassImpl(const PassInfo *P) const { return false; }
60 inline bool ignorablePass(const PassInfo *P) const {
61 // Ignore non-selectable and non-constructible passes! Ignore
63 return P->getPassArgument() == nullptr || *P->getPassArgument() == 0 ||
64 P->getNormalCtor() == nullptr || ignorablePassImpl(P);
67 // Implement the PassRegistrationListener callbacks used to populate our map
69 void passRegistered(const PassInfo *P) override {
70 if (ignorablePass(P)) return;
71 if (findOption(P->getPassArgument()) != getNumOptions()) {
72 errs() << "Two passes with the same argument (-"
73 << P->getPassArgument() << ") attempted to be registered!\n";
74 llvm_unreachable(nullptr);
76 addLiteralOption(P->getPassArgument(), P, P->getPassName());
78 void passEnumerate(const PassInfo *P) override { passRegistered(P); }
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(), ValLessThan);
85 cl::parser<const PassInfo*>::printOptionInfo(O, GlobalWidth);
89 // ValLessThan - Provide a sorting comparator for Values elements...
90 static int ValLessThan(const PassNameParser::OptionInfo *VT1,
91 const PassNameParser::OptionInfo *VT2) {
92 return std::strcmp(VT1->Name, VT2->Name);
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.
102 template<typename Filter>
103 class FilteredPassNameParser : public PassNameParser {
108 bool ignorablePassImpl(const PassInfo *P) const override {
113 ///===----------------------------------------------------------------------===//
114 /// PassArgFilter - A filter for use with PassNameFilterParser that only
115 /// accepts a Pass whose Arg matches certain strings.
119 /// extern const char AllowedPassArgs[] = "-anders_aa -dse";
124 /// FilteredPassNameParser<PassArgFilter<AllowedPassArgs> > >
125 /// PassList(cl::desc("Passes available:"));
127 /// Only the -anders_aa and -dse options will be available to the user.
129 template<const char *Args>
130 class PassArgFilter {
132 bool operator()(const PassInfo &P) const {
133 return(std::strstr(Args, P.getPassArgument()));
137 } // End llvm namespace