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