1 //===- llvm/Support/PassNameParser.h ----------------------------*- C++ -*-===//
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.
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).
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_SUPPORT_PASS_NAME_PARSER_H
17 #define LLVM_SUPPORT_PASS_NAME_PARSER_H
19 #include "Support/CommandLine.h"
20 #include "llvm/Pass.h"
24 //===----------------------------------------------------------------------===//
25 // PassNameParser class - Make use of the pass registration mechanism to
26 // automatically add a command line argument to opt for each pass.
28 class PassNameParser : public PassRegistrationListener,
29 public cl::parser<const PassInfo*> {
32 PassNameParser() : Opt(0) {}
34 void initialize(cl::Option &O) {
36 cl::parser<const PassInfo*>::initialize(O);
38 // Add all of the passes to the map that got initialized before 'this' did.
42 // ignorablePassImpl - Can be overriden in subclasses to refine the list of
43 // which passes we want to include.
45 virtual bool ignorablePassImpl(const PassInfo *P) const { return false; }
47 inline bool ignorablePass(const PassInfo *P) const {
48 // Ignore non-selectable and non-constructible passes! Ignore
50 return P->getPassArgument() == 0 || *P->getPassArgument() == 0 ||
51 (P->getNormalCtor() == 0 && P->getTargetCtor() == 0) ||
55 // Implement the PassRegistrationListener callbacks used to populate our map
57 virtual void passRegistered(const PassInfo *P) {
58 if (ignorablePass(P) || !Opt) return;
59 if (findOption(P->getPassArgument()) != getNumOptions()) {
60 std::cerr << "Two passes with the same argument (-"
61 << P->getPassArgument() << ") attempted to be registered!\n";
64 addLiteralOption(P->getPassArgument(), P, P->getPassName());
65 Opt->addArgument(P->getPassArgument());
67 virtual void passEnumerate(const PassInfo *P) { passRegistered(P); }
69 virtual void passUnregistered(const PassInfo *P) {
70 if (ignorablePass(P) || !Opt) return;
71 assert(findOption(P->getPassArgument()) != getNumOptions() &&
72 "Registered Pass not in the pass map!");
73 removeLiteralOption(P->getPassArgument());
74 Opt->removeArgument(P->getPassArgument());
77 // ValLessThan - Provide a sorting comparator for Values elements...
78 typedef std::pair<const char*,
79 std::pair<const PassInfo*, const char*> > ValType;
80 static bool ValLessThan(const ValType &VT1, const ValType &VT2) {
81 return std::string(VT1.first) < std::string(VT2.first);
84 // printOptionInfo - Print out information about this option. Override the
85 // default implementation to sort the table before we print...
86 virtual void printOptionInfo(const cl::Option &O, unsigned GlobalWidth) const{
87 PassNameParser *PNP = const_cast<PassNameParser*>(this);
88 std::sort(PNP->Values.begin(), PNP->Values.end(), ValLessThan);
89 cl::parser<const PassInfo*>::printOptionInfo(O, GlobalWidth);
94 //===----------------------------------------------------------------------===//
95 // FilteredPassNameParser class - Just like PassNameParser, but filter out
96 // passes that do not have a PassType that includes the flags specified as the
99 template<unsigned Flags>
100 struct FilteredPassNameParser : public PassNameParser {
102 // ignorablePassImpl - Can be overriden in subclasses to refine the list of
103 // which passes we want to include.
105 virtual bool ignorablePassImpl(const PassInfo *P) const {
106 return (P->getPassType() & Flags) == 0;