Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- llvm/IR/OptBisect.h - LLVM Bisect support ----------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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" |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 18 | #include "llvm/Support/ManagedStatic.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 19 | |
| 20 | namespace llvm { |
| 21 | |
| 22 | class Pass; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 23 | |
| 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 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 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 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 36 | /// isEnabled() should return true before calling shouldRunPass(). |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 37 | virtual bool isEnabled() const { return false; } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | /// This class implements a mechanism to disable passes and individual |
| 41 | /// optimizations at compile time based on a command line option |
| 42 | /// (-opt-bisect-limit) in order to perform a bisecting search for |
| 43 | /// optimization-related problems. |
| 44 | class OptBisect : public OptPassGate { |
| 45 | public: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 46 | /// Default constructor, initializes the OptBisect state based on the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 47 | /// -opt-bisect-limit command line argument. |
| 48 | /// |
| 49 | /// By default, bisection is disabled. |
| 50 | /// |
| 51 | /// Clients should not instantiate this class directly. All access should go |
| 52 | /// through LLVMContext. |
| 53 | OptBisect(); |
| 54 | |
| 55 | virtual ~OptBisect() = default; |
| 56 | |
| 57 | /// Checks the bisect limit to determine if the specified pass should run. |
| 58 | /// |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 59 | /// This forwards to checkPass(). |
| 60 | bool shouldRunPass(const Pass *P, StringRef IRDescription) override; |
| 61 | |
| 62 | /// isEnabled() should return true before calling shouldRunPass(). |
| 63 | bool isEnabled() const override { return BisectEnabled; } |
| 64 | |
| 65 | /// Checks the bisect limit to determine if the specified pass should run. |
| 66 | /// |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 67 | /// If the bisect limit is set to -1, the function prints a message describing |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 68 | /// the pass and the bisect number assigned to it and return true. Otherwise, |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 69 | /// the function prints a message with the bisect number assigned to the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 70 | /// pass and indicating whether or not the pass will be run and return true if |
| 71 | /// the bisect limit has not yet been exceeded or false if it has. |
| 72 | /// |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 73 | /// Most passes should not call this routine directly. Instead, they are |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 74 | /// called through helper routines provided by the pass base classes. For |
| 75 | /// instance, function passes should call FunctionPass::skipFunction(). |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 76 | bool checkPass(const StringRef PassName, const StringRef TargetDesc); |
| 77 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 78 | private: |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 79 | bool BisectEnabled = false; |
| 80 | unsigned LastBisectNum = 0; |
| 81 | }; |
| 82 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame^] | 83 | /// Singleton instance of the OptBisect class, so multiple pass managers don't |
| 84 | /// need to coordinate their uses of OptBisect. |
| 85 | extern ManagedStatic<OptBisect> OptBisector; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 86 | } // end namespace llvm |
| 87 | |
| 88 | #endif // LLVM_IR_OPTBISECT_H |