PassInfo.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //===- llvm/PassInfo.h - Pass Info class ------------------------*- 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 defines and implements the PassInfo class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_PASSINFO_H
  13. #define LLVM_PASSINFO_H
  14. #include "llvm/ADT/StringRef.h"
  15. #include <cassert>
  16. #include <vector>
  17. namespace llvm {
  18. class Pass;
  19. //===---------------------------------------------------------------------------
  20. /// PassInfo class - An instance of this class exists for every pass known by
  21. /// the system, and can be obtained from a live Pass by calling its
  22. /// getPassInfo() method. These objects are set up by the RegisterPass<>
  23. /// template.
  24. ///
  25. class PassInfo {
  26. public:
  27. using NormalCtor_t = Pass* (*)();
  28. private:
  29. StringRef PassName; // Nice name for Pass
  30. StringRef PassArgument; // Command Line argument to run this pass
  31. const void *PassID;
  32. const bool IsCFGOnlyPass = false; // Pass only looks at the CFG.
  33. const bool IsAnalysis; // True if an analysis pass.
  34. const bool IsAnalysisGroup; // True if an analysis group.
  35. std::vector<const PassInfo *> ItfImpl; // Interfaces implemented by this pass
  36. NormalCtor_t NormalCtor = nullptr;
  37. public:
  38. /// PassInfo ctor - Do not call this directly, this should only be invoked
  39. /// through RegisterPass.
  40. PassInfo(StringRef name, StringRef arg, const void *pi, NormalCtor_t normal,
  41. bool isCFGOnly, bool is_analysis)
  42. : PassName(name), PassArgument(arg), PassID(pi), IsCFGOnlyPass(isCFGOnly),
  43. IsAnalysis(is_analysis), IsAnalysisGroup(false), NormalCtor(normal) {}
  44. /// PassInfo ctor - Do not call this directly, this should only be invoked
  45. /// through RegisterPass. This version is for use by analysis groups; it
  46. /// does not auto-register the pass.
  47. PassInfo(StringRef name, const void *pi)
  48. : PassName(name), PassID(pi), IsAnalysis(false), IsAnalysisGroup(true) {}
  49. PassInfo(const PassInfo &) = delete;
  50. PassInfo &operator=(const PassInfo &) = delete;
  51. /// getPassName - Return the friendly name for the pass, never returns null
  52. StringRef getPassName() const { return PassName; }
  53. /// getPassArgument - Return the command line option that may be passed to
  54. /// 'opt' that will cause this pass to be run. This will return null if there
  55. /// is no argument.
  56. StringRef getPassArgument() const { return PassArgument; }
  57. /// getTypeInfo - Return the id object for the pass...
  58. /// TODO : Rename
  59. const void *getTypeInfo() const { return PassID; }
  60. /// Return true if this PassID implements the specified ID pointer.
  61. bool isPassID(const void *IDPtr) const { return PassID == IDPtr; }
  62. /// isAnalysisGroup - Return true if this is an analysis group, not a normal
  63. /// pass.
  64. bool isAnalysisGroup() const { return IsAnalysisGroup; }
  65. bool isAnalysis() const { return IsAnalysis; }
  66. /// isCFGOnlyPass - return true if this pass only looks at the CFG for the
  67. /// function.
  68. bool isCFGOnlyPass() const { return IsCFGOnlyPass; }
  69. /// getNormalCtor - Return a pointer to a function, that when called, creates
  70. /// an instance of the pass and returns it. This pointer may be null if there
  71. /// is no default constructor for the pass.
  72. NormalCtor_t getNormalCtor() const {
  73. return NormalCtor;
  74. }
  75. void setNormalCtor(NormalCtor_t Ctor) {
  76. NormalCtor = Ctor;
  77. }
  78. /// createPass() - Use this method to create an instance of this pass.
  79. Pass *createPass() const {
  80. assert((!isAnalysisGroup() || NormalCtor) &&
  81. "No default implementation found for analysis group!");
  82. assert(NormalCtor &&
  83. "Cannot call createPass on PassInfo without default ctor!");
  84. return NormalCtor();
  85. }
  86. /// addInterfaceImplemented - This method is called when this pass is
  87. /// registered as a member of an analysis group with the RegisterAnalysisGroup
  88. /// template.
  89. void addInterfaceImplemented(const PassInfo *ItfPI) {
  90. ItfImpl.push_back(ItfPI);
  91. }
  92. /// getInterfacesImplemented - Return a list of all of the analysis group
  93. /// interfaces implemented by this pass.
  94. const std::vector<const PassInfo*> &getInterfacesImplemented() const {
  95. return ItfImpl;
  96. }
  97. };
  98. } // end namespace llvm
  99. #endif // LLVM_PASSINFO_H