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" |
| 18 | |
| 19 | namespace llvm { |
| 20 | |
| 21 | class Pass; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 22 | |
| 23 | /// Extensions to this class implement mechanisms to disable passes and |
| 24 | /// individual optimizations at compile time. |
| 25 | class OptPassGate { |
| 26 | public: |
| 27 | virtual ~OptPassGate() = default; |
| 28 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 29 | /// IRDescription is a textual description of the IR unit the pass is running |
| 30 | /// over. |
| 31 | virtual bool shouldRunPass(const Pass *P, StringRef IRDescription) { |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | /// isEnabled should return true before calling shouldRunPass |
| 36 | virtual bool isEnabled() const { return false; } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | /// This class implements a mechanism to disable passes and individual |
| 40 | /// optimizations at compile time based on a command line option |
| 41 | /// (-opt-bisect-limit) in order to perform a bisecting search for |
| 42 | /// optimization-related problems. |
| 43 | class OptBisect : public OptPassGate { |
| 44 | public: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 45 | /// Default constructor, initializes the OptBisect state based on the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 46 | /// -opt-bisect-limit command line argument. |
| 47 | /// |
| 48 | /// By default, bisection is disabled. |
| 49 | /// |
| 50 | /// Clients should not instantiate this class directly. All access should go |
| 51 | /// through LLVMContext. |
| 52 | OptBisect(); |
| 53 | |
| 54 | virtual ~OptBisect() = default; |
| 55 | |
| 56 | /// Checks the bisect limit to determine if the specified pass should run. |
| 57 | /// |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 58 | /// 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] | 59 | /// 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] | 60 | /// the function prints a message with the bisect number assigned to the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 61 | /// pass and indicating whether or not the pass will be run and return true if |
| 62 | /// the bisect limit has not yet been exceeded or false if it has. |
| 63 | /// |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 64 | /// Most passes should not call this routine directly. Instead, they are |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 65 | /// called through helper routines provided by the pass base classes. For |
| 66 | /// instance, function passes should call FunctionPass::skipFunction(). |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 67 | bool shouldRunPass(const Pass *P, StringRef IRDescription) override; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 68 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 69 | /// isEnabled should return true before calling shouldRunPass |
| 70 | bool isEnabled() const override { return BisectEnabled; } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 71 | private: |
| 72 | bool checkPass(const StringRef PassName, const StringRef TargetDesc); |
| 73 | |
| 74 | bool BisectEnabled = false; |
| 75 | unsigned LastBisectNum = 0; |
| 76 | }; |
| 77 | |
| 78 | } // end namespace llvm |
| 79 | |
| 80 | #endif // LLVM_IR_OPTBISECT_H |