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 class Pass; 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 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 36 /// isEnabled() should return true before calling shouldRunPass(). 37 virtual bool isEnabled() const { return false; } 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: 46 /// Default constructor. Initializes the state to "disabled". The bisection 47 /// will be enabled by the cl::opt call-back when the command line option 48 /// is processed. 49 /// Clients should not instantiate this class directly. All access should go 50 /// through LLVMContext. 51 OptBisect() = default; 52 53 virtual ~OptBisect() = default; 54 55 /// Checks the bisect limit to determine if the specified pass should run. 56 /// 57 /// This forwards to checkPass(). 58 bool shouldRunPass(const Pass *P, StringRef IRDescription) override; 59 60 /// isEnabled() should return true before calling shouldRunPass(). 61 bool isEnabled() const override { return BisectLimit != Disabled; } 62 63 /// Set the new optimization limit and reset the counter. Passing 64 /// OptBisect::Disabled disables the limiting. 65 void setLimit(int Limit) { 66 BisectLimit = Limit; 67 LastBisectNum = 0; 68 } 69 70 /// Checks the bisect limit to determine if the specified pass should run. 71 /// 72 /// If the bisect limit is set to -1, the function prints a message describing 73 /// the pass and the bisect number assigned to it and return true. Otherwise, 74 /// the function prints a message with the bisect number assigned to the 75 /// pass and indicating whether or not the pass will be run and return true if 76 /// the bisect limit has not yet been exceeded or false if it has. 77 /// 78 /// Most passes should not call this routine directly. Instead, they are 79 /// called through helper routines provided by the pass base classes. For 80 /// instance, function passes should call FunctionPass::skipFunction(). 81 bool checkPass(const StringRef PassName, const StringRef TargetDesc); 82 83 static const int Disabled = std::numeric_limits<int>::max(); 84 85 private: 86 int BisectLimit = Disabled; 87 int LastBisectNum = 0; 88 }; 89 90 /// Singleton instance of the OptBisect class, so multiple pass managers don't 91 /// need to coordinate their uses of OptBisect. 92 OptBisect &getOptBisector(); 93 94 } // end namespace llvm 95 96 #endif // LLVM_IR_OPTBISECT_H 97