OptBisect.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //===- llvm/IR/OptBisect.h - LLVM Bisect support ----------------*- 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. /// \file
  10. /// This file declares the interface for bisecting optimizations.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_IR_OPTBISECT_H
  14. #define LLVM_IR_OPTBISECT_H
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/Support/ManagedStatic.h"
  17. namespace llvm {
  18. class Pass;
  19. /// Extensions to this class implement mechanisms to disable passes and
  20. /// individual optimizations at compile time.
  21. class OptPassGate {
  22. public:
  23. virtual ~OptPassGate() = default;
  24. /// IRDescription is a textual description of the IR unit the pass is running
  25. /// over.
  26. virtual bool shouldRunPass(const Pass *P, StringRef IRDescription) {
  27. return true;
  28. }
  29. /// isEnabled() should return true before calling shouldRunPass().
  30. virtual bool isEnabled() const { return false; }
  31. };
  32. /// This class implements a mechanism to disable passes and individual
  33. /// optimizations at compile time based on a command line option
  34. /// (-opt-bisect-limit) in order to perform a bisecting search for
  35. /// optimization-related problems.
  36. class OptBisect : public OptPassGate {
  37. public:
  38. /// Default constructor, initializes the OptBisect state based on the
  39. /// -opt-bisect-limit command line argument.
  40. ///
  41. /// By default, bisection is disabled.
  42. ///
  43. /// Clients should not instantiate this class directly. All access should go
  44. /// through LLVMContext.
  45. OptBisect();
  46. virtual ~OptBisect() = default;
  47. /// Checks the bisect limit to determine if the specified pass should run.
  48. ///
  49. /// This forwards to checkPass().
  50. bool shouldRunPass(const Pass *P, StringRef IRDescription) override;
  51. /// isEnabled() should return true before calling shouldRunPass().
  52. bool isEnabled() const override { return BisectEnabled; }
  53. /// Checks the bisect limit to determine if the specified pass should run.
  54. ///
  55. /// If the bisect limit is set to -1, the function prints a message describing
  56. /// the pass and the bisect number assigned to it and return true. Otherwise,
  57. /// the function prints a message with the bisect number assigned to the
  58. /// pass and indicating whether or not the pass will be run and return true if
  59. /// the bisect limit has not yet been exceeded or false if it has.
  60. ///
  61. /// Most passes should not call this routine directly. Instead, they are
  62. /// called through helper routines provided by the pass base classes. For
  63. /// instance, function passes should call FunctionPass::skipFunction().
  64. bool checkPass(const StringRef PassName, const StringRef TargetDesc);
  65. private:
  66. bool BisectEnabled = false;
  67. unsigned LastBisectNum = 0;
  68. };
  69. /// Singleton instance of the OptBisect class, so multiple pass managers don't
  70. /// need to coordinate their uses of OptBisect.
  71. extern ManagedStatic<OptBisect> OptBisector;
  72. } // end namespace llvm
  73. #endif // LLVM_IR_OPTBISECT_H