LegacyPassNameParser.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //===- LegacyPassNameParser.h -----------------------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  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.
  13. //
  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).
  20. //
  21. // Note that this is part of the legacy pass manager infrastructure and will be
  22. // (eventually) going away.
  23. //
  24. //===----------------------------------------------------------------------===//
  25. #ifndef LLVM_IR_LEGACYPASSNAMEPARSER_H
  26. #define LLVM_IR_LEGACYPASSNAMEPARSER_H
  27. #include "llvm/ADT/STLExtras.h"
  28. #include "llvm/Pass.h"
  29. #include "llvm/Support/CommandLine.h"
  30. #include "llvm/Support/ErrorHandling.h"
  31. #include "llvm/Support/raw_ostream.h"
  32. #include <cstring>
  33. namespace llvm {
  34. //===----------------------------------------------------------------------===//
  35. // PassNameParser class - Make use of the pass registration mechanism to
  36. // automatically add a command line argument to opt for each pass.
  37. //
  38. class PassNameParser : public PassRegistrationListener,
  39. public cl::parser<const PassInfo*> {
  40. public:
  41. PassNameParser(cl::Option &O);
  42. ~PassNameParser() override;
  43. void initialize() {
  44. cl::parser<const PassInfo*>::initialize();
  45. // Add all of the passes to the map that got initialized before 'this' did.
  46. enumeratePasses();
  47. }
  48. // ignorablePassImpl - Can be overriden in subclasses to refine the list of
  49. // which passes we want to include.
  50. //
  51. virtual bool ignorablePassImpl(const PassInfo *P) const { return false; }
  52. inline bool ignorablePass(const PassInfo *P) const {
  53. // Ignore non-selectable and non-constructible passes! Ignore
  54. // non-optimizations.
  55. return P->getPassArgument() == nullptr || *P->getPassArgument() == 0 ||
  56. P->getNormalCtor() == nullptr || ignorablePassImpl(P);
  57. }
  58. // Implement the PassRegistrationListener callbacks used to populate our map
  59. //
  60. void passRegistered(const PassInfo *P) override {
  61. if (ignorablePass(P)) return;
  62. if (findOption(P->getPassArgument()) != getNumOptions()) {
  63. errs() << "Two passes with the same argument (-"
  64. << P->getPassArgument() << ") attempted to be registered!\n";
  65. llvm_unreachable(nullptr);
  66. }
  67. addLiteralOption(P->getPassArgument(), P, P->getPassName());
  68. }
  69. void passEnumerate(const PassInfo *P) override { passRegistered(P); }
  70. // printOptionInfo - Print out information about this option. Override the
  71. // default implementation to sort the table before we print...
  72. void printOptionInfo(const cl::Option &O, size_t GlobalWidth) const override {
  73. PassNameParser *PNP = const_cast<PassNameParser*>(this);
  74. array_pod_sort(PNP->Values.begin(), PNP->Values.end(), ValLessThan);
  75. cl::parser<const PassInfo*>::printOptionInfo(O, GlobalWidth);
  76. }
  77. private:
  78. // ValLessThan - Provide a sorting comparator for Values elements...
  79. //
  80. // HLSL Change: changed calling convention to __cdecl
  81. static int __cdecl ValLessThan(const PassNameParser::OptionInfo *VT1,
  82. const PassNameParser::OptionInfo *VT2) {
  83. return std::strcmp(VT1->Name, VT2->Name);
  84. }
  85. };
  86. ///===----------------------------------------------------------------------===//
  87. /// FilteredPassNameParser class - Make use of the pass registration
  88. /// mechanism to automatically add a command line argument to opt for
  89. /// each pass that satisfies a filter criteria. Filter should return
  90. /// true for passes to be registered as command-line options.
  91. ///
  92. template<typename Filter>
  93. class FilteredPassNameParser : public PassNameParser {
  94. private:
  95. Filter filter;
  96. public:
  97. bool ignorablePassImpl(const PassInfo *P) const override {
  98. return !filter(*P);
  99. }
  100. };
  101. // //
  102. ///////////////////////////////////////////////////////////////////////////////
  103. /// PassArgFilter - A filter for use with PassNameFilterParser that only
  104. /// accepts a Pass whose Arg matches certain strings.
  105. ///
  106. /// Use like this:
  107. ///
  108. /// extern const char AllowedPassArgs[] = "-anders_aa -dse";
  109. ///
  110. /// static cl::list<
  111. /// const PassInfo*,
  112. /// bool,
  113. /// FilteredPassNameParser<PassArgFilter<AllowedPassArgs> > >
  114. /// PassList(cl::desc("Passes available:"));
  115. ///
  116. /// Only the -anders_aa and -dse options will be available to the user.
  117. ///
  118. template<const char *Args>
  119. class PassArgFilter {
  120. public:
  121. bool operator()(const PassInfo &P) const {
  122. return(std::strstr(Args, P.getPassArgument()));
  123. }
  124. };
  125. } // End llvm namespace
  126. #endif