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 <limits> 19 20 namespace llvm { 21 22 /// Extensions to this class implement mechanisms to disable passes and 23 /// individual optimizations at compile time. 24 class OptPassGate { 25 public: 26 virtual ~OptPassGate() = default; 27 28 /// IRDescription is a textual description of the IR unit the pass is running 29 /// over. 30 virtual bool shouldRunPass(const StringRef PassName, 31 StringRef IRDescription) { 32 return true; 33 } 34 35 /// isEnabled() should return true before calling shouldRunPass(). 36 virtual bool isEnabled() const { return false; } 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: 45 /// Default constructor. Initializes the state to "disabled". The bisection 46 /// will be enabled by the cl::opt call-back when the command line option 47 /// is processed. 48 /// Clients should not instantiate this class directly. All access should go 49 /// through LLVMContext. 50 OptBisect() = default; 51 52 virtual ~OptBisect() = default; 53 54 /// Checks the bisect limit to determine if the specified pass should run. 55 /// 56 /// The method prints the name of the pass, its assigned bisect number, and 57 /// whether or not the pass will be executed. It returns true if the pass 58 /// should run, i.e. if the bisect limit is set to -1 or has not yet been 59 /// exceeded. 60 /// 61 /// Most passes should not call this routine directly. Instead, it is called 62 /// through helper routines provided by the base classes of the pass. For 63 /// instance, function passes should call FunctionPass::skipFunction(). 64 bool shouldRunPass(const StringRef PassName, 65 StringRef IRDescription) override; 66 67 /// isEnabled() should return true before calling shouldRunPass(). 68 bool isEnabled() const override { return BisectLimit != Disabled; } 69 70 /// Set the new optimization limit and reset the counter. Passing 71 /// OptBisect::Disabled disables the limiting. 72 void setLimit(int Limit) { 73 BisectLimit = Limit; 74 LastBisectNum = 0; 75 } 76 77 static const int Disabled = std::numeric_limits<int>::max(); 78 79 private: 80 int BisectLimit = Disabled; 81 int LastBisectNum = 0; 82 }; 83 84 /// Singleton instance of the OptBisect class, so multiple pass managers don't 85 /// need to coordinate their uses of OptBisect. 86 OptPassGate &getGlobalPassGate(); 87 88 } // end namespace llvm 89 90 #endif // LLVM_IR_OPTBISECT_H 91