1 //===- llvm/Support/PassNameParser.h ----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file the PassNameParser and FilteredPassNameParser<> classes, which are
11 // used to add command line arguments to a utility for all of the passes that
12 // 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 //===----------------------------------------------------------------------===//
23 #ifndef LLVM_SUPPORT_PASS_NAME_PARSER_H
24 #define LLVM_SUPPORT_PASS_NAME_PARSER_H
26 #include "Support/CommandLine.h"
27 #include "llvm/Pass.h"
31 //===----------------------------------------------------------------------===//
32 // PassNameParser class - Make use of the pass registration mechanism to
33 // automatically add a command line argument to opt for each pass.
35 class PassNameParser : public PassRegistrationListener,
36 public cl::parser<const PassInfo*> {
39 PassNameParser() : Opt(0) {}
41 void initialize(cl::Option &O) {
43 cl::parser<const PassInfo*>::initialize(O);
45 // Add all of the passes to the map that got initialized before 'this' did.
49 // ignorablePassImpl - Can be overriden in subclasses to refine the list of
50 // which passes we want to include.
52 virtual bool ignorablePassImpl(const PassInfo *P) const { return false; }
54 inline bool ignorablePass(const PassInfo *P) const {
55 // Ignore non-selectable and non-constructible passes! Ignore
57 return P->getPassArgument() == 0 || *P->getPassArgument() == 0 ||
58 (P->getNormalCtor() == 0 && P->getTargetCtor() == 0) ||
62 // Implement the PassRegistrationListener callbacks used to populate our map
64 virtual void passRegistered(const PassInfo *P) {
65 if (ignorablePass(P) || !Opt) return;
66 if (findOption(P->getPassArgument()) != getNumOptions()) {
67 std::cerr << "Two passes with the same argument (-"
68 << P->getPassArgument() << ") attempted to be registered!\n";
71 addLiteralOption(P->getPassArgument(), P, P->getPassName());
72 Opt->addArgument(P->getPassArgument());
74 virtual void passEnumerate(const PassInfo *P) { passRegistered(P); }
76 virtual void passUnregistered(const PassInfo *P) {
77 if (ignorablePass(P) || !Opt) return;
78 assert(findOption(P->getPassArgument()) != getNumOptions() &&
79 "Registered Pass not in the pass map!");
80 removeLiteralOption(P->getPassArgument());
81 Opt->removeArgument(P->getPassArgument());
84 // ValLessThan - Provide a sorting comparator for Values elements...
85 typedef std::pair<const char*,
86 std::pair<const PassInfo*, const char*> > ValType;
87 static bool ValLessThan(const ValType &VT1, const ValType &VT2) {
88 return std::string(VT1.first) < std::string(VT2.first);
91 // printOptionInfo - Print out information about this option. Override the
92 // default implementation to sort the table before we print...
93 virtual void printOptionInfo(const cl::Option &O, unsigned GlobalWidth) const{
94 PassNameParser *PNP = const_cast<PassNameParser*>(this);
95 std::sort(PNP->Values.begin(), PNP->Values.end(), ValLessThan);
96 cl::parser<const PassInfo*>::printOptionInfo(O, GlobalWidth);
101 //===----------------------------------------------------------------------===//
102 // FilteredPassNameParser class - Just like PassNameParser, but filter out
103 // passes that do not have a PassType that includes the flags specified as the
104 // template argument.
106 template<unsigned Flags>
107 struct FilteredPassNameParser : public PassNameParser {
109 // ignorablePassImpl - Can be overriden in subclasses to refine the list of
110 // which passes we want to include.
112 virtual bool ignorablePassImpl(const PassInfo *P) const {
113 return (P->getPassType() & Flags) == 0;