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"
23 //===----------------------------------------------------------------------===//
24 // PassNameParser class - Make use of the pass registration mechanism to
25 // automatically add a command line argument to opt for each pass.
27 class PassNameParser : public PassRegistrationListener,
28 public cl::parser<const PassInfo*> {
31 PassNameParser() : Opt(0) {}
33 void initialize(cl::Option &O) {
35 cl::parser<const PassInfo*>::initialize(O);
37 // Add all of the passes to the map that got initialized before 'this' did.
41 // ignorablePassImpl - Can be overriden in subclasses to refine the list of
42 // which passes we want to include.
44 virtual bool ignorablePassImpl(const PassInfo *P) const { return false; }
46 inline bool ignorablePass(const PassInfo *P) const {
47 // Ignore non-selectable and non-constructible passes! Ignore
49 return P->getPassArgument() == 0 ||
50 (P->getNormalCtor() == 0 && P->getDataCtor() == 0 &&
51 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 assert(findOption(P->getPassArgument()) == getNumOptions() &&
60 "Two passes with the same argument attempted to be registered!");
61 addLiteralOption(P->getPassArgument(), P, P->getPassName());
62 Opt->addArgument(P->getPassArgument());
64 virtual void passEnumerate(const PassInfo *P) { passRegistered(P); }
66 virtual void passUnregistered(const PassInfo *P) {
67 if (ignorablePass(P) || !Opt) return;
68 assert(findOption(P->getPassArgument()) != getNumOptions() &&
69 "Registered Pass not in the pass map!");
70 removeLiteralOption(P->getPassArgument());
71 Opt->removeArgument(P->getPassArgument());
74 // ValLessThan - Provide a sorting comparator for Values elements...
75 typedef std::pair<const char*,
76 std::pair<const PassInfo*, const char*> > ValType;
77 static bool ValLessThan(const ValType &VT1, const ValType &VT2) {
78 return std::string(VT1.first) < std::string(VT2.first);
81 // printOptionInfo - Print out information about this option. Override the
82 // default implementation to sort the table before we print...
83 virtual void printOptionInfo(const cl::Option &O, unsigned GlobalWidth) const{
84 PassNameParser *PNP = const_cast<PassNameParser*>(this);
85 std::sort(PNP->Values.begin(), PNP->Values.end(), ValLessThan);
86 cl::parser<const PassInfo*>::printOptionInfo(O, GlobalWidth);
91 //===----------------------------------------------------------------------===//
92 // FilteredPassNameParser class - Just like PassNameParser, but filter out
93 // passes that do not have a PassType that includes the flags specified as the
96 template<unsigned Flags>
97 struct FilteredPassNameParser : public PassNameParser {
99 // ignorablePassImpl - Can be overriden in subclasses to refine the list of
100 // which passes we want to include.
102 virtual bool ignorablePassImpl(const PassInfo *P) const {
103 return (P->getPassType() & Flags) == 0;