OptBisect.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 "llvm/Support/ManagedStatic.h"
  22. #include <limits>
  23. namespace llvm {
  24. class Pass;
  25. /// Extensions to this class implement mechanisms to disable passes and
  26. /// individual optimizations at compile time.
  27. class OptPassGate {
  28. public:
  29. virtual ~OptPassGate() = default;
  30. /// IRDescription is a textual description of the IR unit the pass is running
  31. /// over.
  32. virtual bool shouldRunPass(const Pass *P, 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 Pass *P, StringRef IRDescription) override;
  55. /// isEnabled() should return true before calling shouldRunPass().
  56. bool isEnabled() const override { return BisectLimit != Disabled; }
  57. /// Set the new optimization limit and reset the counter. Passing
  58. /// OptBisect::Disabled disables the limiting.
  59. void setLimit(int Limit) {
  60. BisectLimit = Limit;
  61. LastBisectNum = 0;
  62. }
  63. /// Checks the bisect limit to determine if the specified pass should run.
  64. ///
  65. /// If the bisect limit is set to -1, the function prints a message describing
  66. /// the pass and the bisect number assigned to it and return true. Otherwise,
  67. /// the function prints a message with the bisect number assigned to the
  68. /// pass and indicating whether or not the pass will be run and return true if
  69. /// the bisect limit has not yet been exceeded or false if it has.
  70. ///
  71. /// Most passes should not call this routine directly. Instead, they are
  72. /// called through helper routines provided by the pass base classes. For
  73. /// instance, function passes should call FunctionPass::skipFunction().
  74. bool checkPass(const StringRef PassName, const StringRef TargetDesc);
  75. static const int Disabled = std::numeric_limits<int>::max();
  76. private:
  77. int BisectLimit = Disabled;
  78. int LastBisectNum = 0;
  79. };
  80. /// Singleton instance of the OptBisect class, so multiple pass managers don't
  81. /// need to coordinate their uses of OptBisect.
  82. extern ManagedStatic<OptBisect> OptBisector;
  83. } // end namespace llvm
  84. #endif // LLVM_IR_OPTBISECT_H
  85. #ifdef __GNUC__
  86. #pragma GCC diagnostic pop
  87. #endif