LegacyPassNameParser.h 3.9 KB

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