LegacyPassNameParser.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #ifndef LLVM_IR_LEGACYPASSNAMEPARSER_H
  25. #define LLVM_IR_LEGACYPASSNAMEPARSER_H
  26. #include "llvm/ADT/STLExtras.h"
  27. #include "llvm/Pass.h"
  28. #include "llvm/Support/CommandLine.h"
  29. #include "llvm/Support/ErrorHandling.h"
  30. #include "llvm/Support/raw_ostream.h"
  31. #include <cstring>
  32. namespace llvm {
  33. //===----------------------------------------------------------------------===//
  34. // PassNameParser class - Make use of the pass registration mechanism to
  35. // automatically add a command line argument to opt for each pass.
  36. //
  37. class PassNameParser : public PassRegistrationListener,
  38. public cl::parser<const PassInfo*> {
  39. public:
  40. PassNameParser(cl::Option &O);
  41. ~PassNameParser() override;
  42. void initialize() {
  43. cl::parser<const PassInfo*>::initialize();
  44. // Add all of the passes to the map that got initialized before 'this' did.
  45. enumeratePasses();
  46. }
  47. // ignorablePassImpl - Can be overriden in subclasses to refine the list of
  48. // which passes we want to include.
  49. //
  50. virtual bool ignorablePassImpl(const PassInfo *P) const { return false; }
  51. inline bool ignorablePass(const PassInfo *P) const {
  52. // Ignore non-selectable and non-constructible passes! Ignore
  53. // non-optimizations.
  54. return P->getPassArgument().empty() || P->getNormalCtor() == nullptr ||
  55. ignorablePassImpl(P);
  56. }
  57. // Implement the PassRegistrationListener callbacks used to populate our map
  58. //
  59. void passRegistered(const PassInfo *P) override {
  60. if (ignorablePass(P)) return;
  61. if (findOption(P->getPassArgument().data()) != getNumOptions()) {
  62. errs() << "Two passes with the same argument (-"
  63. << P->getPassArgument() << ") attempted to be registered!\n";
  64. llvm_unreachable(nullptr);
  65. }
  66. addLiteralOption(P->getPassArgument().data(), P, P->getPassName().data());
  67. }
  68. void passEnumerate(const PassInfo *P) override { passRegistered(P); }
  69. // printOptionInfo - Print out information about this option. Override the
  70. // default implementation to sort the table before we print...
  71. void printOptionInfo(const cl::Option &O, size_t GlobalWidth) const override {
  72. PassNameParser *PNP = const_cast<PassNameParser*>(this);
  73. array_pod_sort(PNP->Values.begin(), PNP->Values.end(), ValCompare);
  74. cl::parser<const PassInfo*>::printOptionInfo(O, GlobalWidth);
  75. }
  76. private:
  77. // ValCompare - Provide a sorting comparator for Values elements...
  78. static int ValCompare(const PassNameParser::OptionInfo *VT1,
  79. const PassNameParser::OptionInfo *VT2) {
  80. return VT1->Name.compare(VT2->Name);
  81. }
  82. };
  83. } // End llvm namespace
  84. #endif