1//===- LegacyPassNameParser.h -----------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the PassNameParser and FilteredPassNameParser<> classes,
10// which are used to add command line arguments to a utility for all of the
11// passes that have been registered into the system.
12//
13// The PassNameParser class adds ALL passes linked into the system (that are
14// creatable) as command line arguments to the tool (when instantiated with the
15// appropriate command line option template). The FilteredPassNameParser<>
16// template is used for the same purposes as PassNameParser, except that it only
17// includes passes that have a PassType that are compatible with the filter
18// (which is the template argument).
19//
20// Note that this is part of the legacy pass manager infrastructure and will be
21// (eventually) going away.
22//
23//===----------------------------------------------------------------------===//
24
25#ifndef LLVM_IR_LEGACYPASSNAMEPARSER_H
26#define LLVM_IR_LEGACYPASSNAMEPARSER_H
27
28#include "llvm/ADT/DenseSet.h"
29#include "llvm/ADT/STLExtras.h"
30#include "llvm/Pass.h"
31#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Compiler.h"
33#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/raw_ostream.h"
35#include <cstring>
36
37namespace llvm {
38
39//===----------------------------------------------------------------------===//
40// PassNameParser class - Make use of the pass registration mechanism to
41// automatically add a command line argument to opt for each pass.
42//
43class LLVM_ABI PassNameParser : public PassRegistrationListener,
44 public cl::parser<const PassInfo *> {
45public:
46 PassNameParser(cl::Option &O);
47 ~PassNameParser() override;
48
49 void initialize() {
50 cl::parser<const PassInfo*>::initialize();
51
52 // Add all of the passes to the map that got initialized before 'this' did.
53 enumeratePasses();
54 }
55
56 // ignorablePassImpl - Can be overriden in subclasses to refine the list of
57 // which passes we want to include.
58 //
59 virtual bool ignorablePassImpl(const PassInfo *P) const { return false; }
60
61 inline bool ignorablePass(const PassInfo *P) const {
62 // Ignore non-selectable and non-constructible passes! Ignore
63 // non-optimizations.
64 return P->getPassArgument().empty() || P->getNormalCtor() == nullptr ||
65 ignorablePassImpl(P);
66 }
67
68 // Implement the PassRegistrationListener callbacks used to populate our map
69 //
70 void passRegistered(const PassInfo *P) override {
71 if (ignorablePass(P)) return;
72 if (!PassArguments.insert(V: P->getPassArgument()).second) {
73 errs() << "Two passes with the same argument (-"
74 << P->getPassArgument() << ") attempted to be registered!\n";
75 llvm_unreachable(nullptr);
76 }
77 addLiteralOption(Name: P->getPassArgument().data(), V: P, HelpStr: P->getPassName().data());
78 }
79 void passEnumerate(const PassInfo *P) override { passRegistered(P); }
80
81 // printOptionInfo - Print out information about this option. Override the
82 // default implementation to sort the table before we print...
83 void printOptionInfo(const cl::Option &O, size_t GlobalWidth) const override {
84 PassNameParser *PNP = const_cast<PassNameParser*>(this);
85 array_pod_sort(Start: PNP->Values.begin(), End: PNP->Values.end(), Compare: ValCompare);
86 cl::parser<const PassInfo*>::printOptionInfo(O, GlobalWidth);
87 }
88
89private:
90 // The arguments of the passes added so far.
91 DenseSet<StringRef> PassArguments;
92
93 // ValCompare - Provide a sorting comparator for Values elements...
94 static int ValCompare(const PassNameParser::OptionInfo *VT1,
95 const PassNameParser::OptionInfo *VT2) {
96 return VT1->Name.compare(RHS: VT2->Name);
97 }
98};
99
100} // End llvm namespace
101
102#endif
103