OptBisect.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/IR/OptBisect.h - LLVM Bisect support ----------------*- 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. /// \file
  15. /// This file declares the interface for bisecting optimizations.
  16. ///
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_IR_OPTBISECT_H
  19. #define LLVM_IR_OPTBISECT_H
  20. #include "llvm/ADT/StringRef.h"
  21. #include <limits>
  22. namespace llvm {
  23. class Pass;
  24. /// Extensions to this class implement mechanisms to disable passes and
  25. /// individual optimizations at compile time.
  26. class OptPassGate {
  27. public:
  28. virtual ~OptPassGate() = default;
  29. /// IRDescription is a textual description of the IR unit the pass is running
  30. /// over.
  31. virtual bool shouldRunPass(const StringRef PassName,
  32. StringRef IRDescription) {
  33. return true;
  34. }
  35. /// isEnabled() should return true before calling shouldRunPass().
  36. virtual bool isEnabled() const { return false; }
  37. };
  38. /// This class implements a mechanism to disable passes and individual
  39. /// optimizations at compile time based on a command line option
  40. /// (-opt-bisect-limit) in order to perform a bisecting search for
  41. /// optimization-related problems.
  42. class OptBisect : public OptPassGate {
  43. public:
  44. /// Default constructor. Initializes the state to "disabled". The bisection
  45. /// will be enabled by the cl::opt call-back when the command line option
  46. /// is processed.
  47. /// Clients should not instantiate this class directly. All access should go
  48. /// through LLVMContext.
  49. OptBisect() = default;
  50. virtual ~OptBisect() = default;
  51. /// Checks the bisect limit to determine if the specified pass should run.
  52. ///
  53. /// This forwards to checkPass().
  54. bool shouldRunPass(const StringRef PassName,
  55. StringRef IRDescription) override;
  56. /// isEnabled() should return true before calling shouldRunPass().
  57. bool isEnabled() const override { return BisectLimit != Disabled; }
  58. /// Set the new optimization limit and reset the counter. Passing
  59. /// OptBisect::Disabled disables the limiting.
  60. void setLimit(int Limit) {
  61. BisectLimit = Limit;
  62. LastBisectNum = 0;
  63. }
  64. /// Checks the bisect limit to determine if the specified pass should run.
  65. ///
  66. /// If the bisect limit is set to -1, the function prints a message describing
  67. /// the pass and the bisect number assigned to it and return true. Otherwise,
  68. /// the function prints a message with the bisect number assigned to the
  69. /// pass and indicating whether or not the pass will be run and return true if
  70. /// the bisect limit has not yet been exceeded or false if it has.
  71. ///
  72. /// Most passes should not call this routine directly. Instead, they are
  73. /// called through helper routines provided by the pass base classes. For
  74. /// instance, function passes should call FunctionPass::skipFunction().
  75. bool checkPass(const StringRef PassName, const StringRef TargetDesc);
  76. static const int Disabled = std::numeric_limits<int>::max();
  77. private:
  78. int BisectLimit = Disabled;
  79. int LastBisectNum = 0;
  80. };
  81. /// Singleton instance of the OptBisect class, so multiple pass managers don't
  82. /// need to coordinate their uses of OptBisect.
  83. OptPassGate &getGlobalPassGate();
  84. } // end namespace llvm
  85. #endif // LLVM_IR_OPTBISECT_H
  86. #ifdef __GNUC__
  87. #pragma GCC diagnostic pop
  88. #endif